Skip to content

Instantly share code, notes, and snippets.

@marcellodesales
Created January 24, 2014 17:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcellodesales/e0832b08c46729560049 to your computer and use it in GitHub Desktop.
Save marcellodesales/e0832b08c46729560049 to your computer and use it in GitHub Desktop.
Proxy Multipart Forms using Jersey RESTFul resource server and Client.
/**
* Proxies multipart forms to another server using Jersey for any path other than "proxy"
*/
@POST
@Path("/{path: (?!proxy).*}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(FormDataMultiPart multiPart, @Context HttpServletRequest httpRequest) throws Exception {
ClientConfig config = new DefaultClientConfig();
// builds a request with a given transaction id. Just uses a Jersey Client with the given config and target URL in the httpRequest
Map<String, WebResource.Builder> proxyRequestToCfp = makeRequest(httpRequest, config, HttpMethod.POST);
// transaction id
String tid = proxyRequestToCfp.keySet().iterator().next();
WebResource.Builder resource = proxyRequestToCfp.get(tid);
resource.type(MediaType.MULTIPART_FORM_DATA + " ; charset=ISO-8859-1");
// post the multipart form to the client.
ClientResponse cfpResponseToProxy = resource.post(ClientResponse.class, multiPart);
Response.ResponseBuilder clientResponse = makeResponse(tid, cfpResponseToProxy);
return clientResponse.build();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment