-
-
Save marcellodesales/e0832b08c46729560049 to your computer and use it in GitHub Desktop.
Proxy Multipart Forms using Jersey RESTFul resource server and Client.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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