Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

XWIKI-7228: Fail to download extension file from XR when provided as URL

  • Loading branch information...
commit b29afb84b11227b39a7a3ace878597fce4bfda38 1 parent 9f2ee0b
Thomas Mortagne authored December 05, 2011
6  ...src/main/java/org/xwiki/extension/repository/xwiki/internal/resources/AbstractExtensionRESTResource.java
@@ -471,9 +471,9 @@ protected ResponseBuilder getAttachmentResponse(Attachment xwikiAttachment) thro
471 471
 
472 472
         ResponseBuilder response = Response.ok();
473 473
 
474  
-        response.type(xwikiAttachment.getMimeType());
475  
-        response.entity(xwikiAttachment.getContent());
476  
-        response.header("Content-Disposition", "attachment; filename=\"" + xwikiAttachment.getFilename() + "\"");
  474
+        response = response.type(xwikiAttachment.getMimeType());
  475
+        response = response.entity(xwikiAttachment.getContent());
  476
+        response = response.header("content-disposition", "attachment; filename=\"" + xwikiAttachment.getFilename() + "\"");
477 477
 
478 478
         return response;
479 479
     }
29  .../main/java/org/xwiki/extension/repository/xwiki/internal/resources/ExtensionVersionFileRESTResource.java
@@ -19,8 +19,11 @@
19 19
  */
20 20
 package org.xwiki.extension.repository.xwiki.internal.resources;
21 21
 
22  
-import java.net.URI;
  22
+import java.io.IOException;
  23
+import java.net.HttpURLConnection;
23 24
 import java.net.URISyntaxException;
  25
+import java.net.URL;
  26
+import java.net.URLConnection;
24 27
 
25 28
 import javax.inject.Inject;
26 29
 import javax.inject.Named;
@@ -33,6 +36,8 @@
33 36
 import javax.ws.rs.core.Response.Status;
34 37
 
35 38
 import org.apache.commons.lang3.StringUtils;
  39
+import org.restlet.data.MediaType;
  40
+import org.restlet.resource.InputRepresentation;
36 41
 import org.xwiki.component.annotation.Component;
37 42
 import org.xwiki.extension.repository.xwiki.Resources;
38 43
 import org.xwiki.extension.repository.xwiki.internal.XWikiRepositoryModel;
@@ -66,7 +71,7 @@
66 71
     @GET
67 72
     public Response downloadExtension(@PathParam(Resources.PPARAM_EXTENSIONID) String extensionId,
68 73
         @PathParam(Resources.PPARAM_EXTENSIONVERSION) String extensionVersion) throws XWikiException, QueryException,
69  
-        URISyntaxException
  74
+        URISyntaxException, IOException
70 75
     {
71 76
         Document extensionDocument = getExtensionDocument(extensionId);
72 77
 
@@ -100,7 +105,25 @@ public Response downloadExtension(@PathParam(Resources.PPARAM_EXTENSIONID) Strin
100 105
                 response = getAttachmentResponse(xwikiAttachment);
101 106
             } else if (ResourceType.URL.equals(resourceReference.getType())) {
102 107
                 // It's an URL
103  
-                response = Response.created(new URI(resourceReference.getReference()));
  108
+                URL url = new URL(resourceReference.getReference());
  109
+
  110
+                // TODO: find a proper way to do a perfect proxy of the URL without directly using Restlet classes.
  111
+                // It's a real pain since Restlet does not properly support some standard headers (like the charset in
  112
+                // the content-type for example) when given as it is in the Response
  113
+
  114
+                URLConnection connection = url.openConnection();
  115
+
  116
+                if (connection instanceof HttpURLConnection) {
  117
+                    HttpURLConnection httpConnection = (HttpURLConnection) connection;
  118
+                    response = Response.status(httpConnection.getResponseCode());
  119
+                } else {
  120
+                    response = Response.ok();
  121
+                }
  122
+
  123
+                InputRepresentation content =
  124
+                    new InputRepresentation(connection.getInputStream(), new MediaType(connection.getContentType()),
  125
+                        connection.getContentLength());
  126
+                response.entity(content);
104 127
             } else {
105 128
                 throw new WebApplicationException(Status.NOT_FOUND);
106 129
             }

0 notes on commit b29afb8

Please sign in to comment.
Something went wrong with that request. Please try again.