* @throws GadgetException
*/
protected HttpRequest buildHttpRequest(HttpServletRequest request) throws GadgetException {
String urlStr = getParameter(request, Param.URL.getKey(), null);
if (urlStr == null) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, Param.URL.getKey()
+ " parameter is missing.", HttpResponse.SC_BAD_REQUEST);
}
Uri url;
try {
url = ServletUtil.validateUrl(Uri.parse(urlStr));
} catch (IllegalArgumentException e) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER, "Invalid "
+ Param.URL.getKey() + " parameter", HttpResponse.SC_BAD_REQUEST);
}
SecurityToken token = AuthInfoUtil.getSecurityTokenFromRequest(request);
String container = null;
Uri gadgetUri = null;
if ("1".equals(getParameter(request, MULTI_PART_FORM_POST, null))) {
// This endpoint is being used by the proxied-form-post feature.
// Require a token.
if (token == null) {
throw new GadgetException(GadgetException.Code.INVALID_SECURITY_TOKEN);
}
}
// If we have a token, we should use it.
if (token != null && !token.isAnonymous()) {
container = token.getContainer();
String appurl = token.getAppUrl();
if (appurl != null) {
gadgetUri = Uri.parse(appurl);
}
} else {
container = getContainer(request);
String gadgetUrl = getParameter(request, Param.GADGET.getKey(), null);
if (gadgetUrl != null) {
gadgetUri = Uri.parse(gadgetUrl);
}
}
HttpRequest req = new HttpRequest(url).setMethod(getParameter(request, METHOD_PARAM, "GET"))
.setContainer(container).setGadget(gadgetUri);
if ("POST".equals(req.getMethod()) || "PUT".equals(req.getMethod())) {
setPostData(container, request, req);
}
String headerData = getParameter(request, HEADERS_PARAM, "");
if (headerData.length() > 0) {
String[] headerList = StringUtils.split(headerData, '&');
for (String header : headerList) {
String[] parts = StringUtils.splitPreserveAllTokens(header, '=');
if (parts.length != 2) {
throw new GadgetException(GadgetException.Code.INVALID_PARAMETER,
"Malformed header param specified:" + header, HttpResponse.SC_BAD_REQUEST);
}
String headerName = Utf8UrlCoder.decode(parts[0]);
if (!HttpRequestHandler.BAD_HEADERS.contains(headerName.toUpperCase())) {
req.addHeader(headerName, Utf8UrlCoder.decode(parts[1]));