private Request prepareRequest(HttpServletRequest req,
Request.MethodType methodType, String url,
final ApplicationLinkRequestFactory requestFactory)
throws CredentialsRequiredException, IOException
{
Request request = requestFactory.createRequest(methodType, url);
// remove xsrf token check on the destination. Assumes this servlet requires an xsrf token
request.setHeader("X-Atlassian-Token", "no-check");
// forward the original ip or pass on already forwarded ip so logging is accurate.
String xForward = req.getHeader("X-Forwarded-For");
request.setHeader("X-Forwarded-For", xForward != null ? xForward : req.getRemoteAddr());
if (methodType == Request.MethodType.POST)
{
String ctHeader = req.getHeader("Content-Type");
if (ctHeader != null)
{
request.setHeader("Content-Type", ctHeader);
}
if (ctHeader != null && ctHeader.contains("application/x-www-form-urlencoded"))
{
List<String> params = new ArrayList<String>();
final Map<String, String[]> parameterMap = (Map<String, String[]>) req.getParameterMap();
for (String name : parameterMap.keySet())
{
if (RESERVED_PARAMS.contains(name))
{
continue;
}
params.add(name);
params.add(req.getParameter(name));
}
request.addRequestParameters((String[]) params.toArray(new String[params.size()]));
}
else
{
String enc = req.getCharacterEncoding();
String str = IOUtils.toString(req.getInputStream(), (enc == null ? "ISO8859_1" : enc));
request.setRequestBody(str);
}
}
return request;
}