Please can someone give me a hint over my question below. Thanks.
We have a XML content coming from a server in the request using content type "application/x-www-form-urlencoded".
We tried to read the request as XML in Groovy and received following error. [Fatal Error] :1:1: Premature end of file. 2015-11-16 17:15:26,777 errors.GrailsExceptionResolver SAXParseException occurred when processing request: [POST] /games/api/v1/endpoint Premature end of file.. Stacktrace follows:
unfortunately we cannot change the content type at the server.
We have tried to create a proxy to get the request from the server and change the content type to "application/xml" and then redirect to the actual endpoint so that we can read the XML properly.
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping(value = "/e2-proxy", method = RequestMethod.POST)
String home(HttpServletRequest request) {
InputStreamEntity requestEntity = null;
try {
requestEntity = new InputStreamEntity(request.getInputStream(),ContentType.create("text/xml", Consts.UTF_8));
} catch (IOException e1) {
e1.printStackTrace();
}
HttpPost httppost = new HttpPost("URL");
httppost.setEntity(requestEntity);
requestEntity.setChunked(true);
requestEntity.setContentType("text/xml");
httppost.setHeader(HTTP.CONTENT_TYPE, "text/xml");
HttpClient client = HttpClients.createDefault();
try {
client.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
Used Apache HttpClient for the request redirection. However We still couldnt read the XML after changing the content type.
Please help to validate the solution, to know what must be going wrong, also suggest any alternatives.