2222import eu .fusepool .p3 .vocab .ELDP ;
2323import java .io .ByteArrayInputStream ;
2424import java .io .ByteArrayOutputStream ;
25- import java .io .File ;
2625import java .io .IOException ;
2726import java .io .InputStream ;
2827import java .io .StringWriter ;
29- import java .net .MalformedURLException ;
3028import java .net .URI ;
3129import java .net .URISyntaxException ;
32- import java .net .URL ;
3330import java .util .Enumeration ;
3431import java .util .HashSet ;
3532import java .util .Iterator ;
5855import org .apache .http .client .methods .HttpEntityEnclosingRequestBase ;
5956import org .apache .http .client .methods .HttpGet ;
6057import org .apache .http .client .methods .HttpPost ;
61- import org .apache .http .client .methods .HttpPut ;
6258import org .apache .http .client .methods .HttpUriRequest ;
6359import org .apache .http .entity .ByteArrayEntity ;
64- import org .apache .http .entity .ContentType ;
65- import org .apache .http .entity .FileEntity ;
6660import org .apache .http .impl .client .CloseableHttpClient ;
6761import org .apache .http .impl .client .HttpClientBuilder ;
6862import org .apache .http .protocol .HttpContext ;
@@ -79,10 +73,8 @@ public class ProxyHandler extends AbstractHandler {
7973 private final CloseableHttpClient httpclient ;
8074 private final Parser parser = Parser .getInstance ();
8175 private final Serializer serializer = Serializer .getInstance ();
82- boolean firstRequest = true ;
83- private final String backendAutoConfiguration ;
8476
85- ProxyHandler (String targetBaseUri , String backendAutoConfiguration ) {
77+ ProxyHandler (String targetBaseUri ) {
8678 if (targetBaseUri .endsWith ("/" )) {
8779 this .targetBaseUri = targetBaseUri .substring (0 , targetBaseUri .length () - 1 );
8880 } else {
@@ -91,7 +83,6 @@ public class ProxyHandler extends AbstractHandler {
9183 final HttpClientBuilder hcb = HttpClientBuilder .create ();
9284 hcb .setRedirectStrategy (new NeverRedirectStrategy ());
9385 httpclient = hcb .build ();
94- this .backendAutoConfiguration = backendAutoConfiguration ;
9586 }
9687
9788 @ Override
@@ -100,18 +91,6 @@ public void handle(String target, Request baseRequest,
10091 throws IOException , ServletException {
10192 final String targetUriString = targetBaseUri + inRequest .getRequestURI ();
10293 final String requestUri = getFullRequestUrl (inRequest );
103- if (firstRequest ) {
104- if (!"no-auto-config" .equals (inRequest .getHeader ("X-Fusepool-Proxy" ))) {
105- synchronized (this ) {
106- if (firstRequest ) {
107- if (backendAutoConfiguration != null ) {
108- autoCofigureBackend (requestUri );
109- }
110- firstRequest = false ;
111- }
112- }
113- }
114- }
11594 //System.out.println(targetUriString);
11695 final URI targetUri ;
11796 try {
@@ -138,7 +117,7 @@ public String getMethod() {
138117 final Enumeration <String > headerNames = baseRequest .getHeaderNames ();
139118 while (headerNames .hasMoreElements ()) {
140119 final String headerName = headerNames .nextElement ();
141- if (headerName .equalsIgnoreCase ("Content-Length" )
120+ if (headerName .equalsIgnoreCase ("Content-Length" )
142121 || headerName .equalsIgnoreCase ("X-Fusepool-Proxy" )
143122 || headerName .equalsIgnoreCase ("Transfer-Encoding" )) {
144123 continue ;
@@ -159,7 +138,7 @@ public String getMethod() {
159138 if (inEntityBytes .length > 0 ) {
160139 outRequest .setEntity (new ByteArrayEntity (inEntityBytes ));
161140 }
162- final CloseableHttpResponse inResponse = httpclient .execute (outRequest );
141+ final CloseableHttpResponse inResponse = httpclient .execute (outRequest );
163142 try {
164143 outResponse .setStatus (inResponse .getStatusLine ().getStatusCode ());
165144 final Header [] inResponseHeaders = inResponse .getAllHeaders ();
@@ -260,13 +239,13 @@ private void startTransformation(final String resourceUri,
260239
261240 @ Override
262241 public void run () {
263- Transformer transformer = new TransformerClientImpl (transformerUri );
242+ Transformer transformer = new TransformerClientImpl (transformerUri );
264243 Entity entity = new InputStreamEntity () {
265244
266245 @ Override
267246 public MimeType getType () {
268247 try {
269- for (Header h : requestHeaders ) {
248+ for (Header h : requestHeaders ) {
270249 if (h .getName ().equalsIgnoreCase ("Content-Type" )) {
271250 return new MimeType (h .getValue ());
272251 }
@@ -319,7 +298,7 @@ public URI getContentLocation() {
319298 } catch (IOException ex ) {
320299 throw new RuntimeException (ex );
321300 }
322-
301+
323302 }
324303
325304 }).start ();
@@ -354,55 +333,6 @@ private void post(String ldpcUri, HttpEntity entity, String mediaType,
354333 }
355334 }
356335
357- private void autoCofigureBackend (String requestUri ) throws MalformedURLException , IOException {
358- URL baseUrl = new URL (new URL (requestUri ), "/" );
359- File directory = new File (backendAutoConfiguration );
360- if (!directory .exists ()) {
361- log .warn (directory .getAbsolutePath () + " does not exist, ignoring." );
362- return ;
363- }
364- putFileOrDir (baseUrl , directory );
365- }
366-
367- private void putFileOrDir (URL url , File file ) throws MalformedURLException , IOException {
368- if (!file .isDirectory ()) {
369- putFile (url , file );
370- } else {
371- final String urlString = url .toString ();
372- final URL baseUrl = urlString .endsWith ("/" ) ? url : new URL (urlString +"/" );
373- for (File child : file .listFiles ()) {
374- putFileOrDir (new URL (baseUrl , child .getName ()), child );
375- }
376- }
377- }
378-
379- private void putFile (URL url , File file ) throws IOException {
380- try {
381- HttpPut httpPut = new HttpPut (url .toURI ());
382- //again, while we could also post directly to the proxied server, this would
383- //require twaeking host header and possibly request path
384- //so we call back to the proxy and use this header to allow a transformation loop
385- httpPut .setHeader ("X-Fusepool-Proxy" , "no-auto-config" );
386- ContentType contentType = guessContentType (file );
387- HttpEntity entity = new FileEntity (file , contentType );
388- httpPut .setEntity (entity );
389- try (CloseableHttpResponse response = httpclient .execute (httpPut )) {
390- if (response .getStatusLine ().getStatusCode () != 201 ) {
391- log .warn ("Response to PUT request to backend resulted in: "
392- + response .getStatusLine () + " rather than 201. URI: " + url );
393- }
394- }
395-
396- } catch (URISyntaxException ex ) {
397- throw new RuntimeException (ex );
398- }
399- }
400-
401- private static ContentType guessContentType (File file ) {
402- //TODO be a bit more sophisticated
403- return ContentType .create ("text/turtle" );
404- }
405-
406336 private static class NeverRedirectStrategy implements RedirectStrategy {
407337
408338 public NeverRedirectStrategy () {
0 commit comments