public void fetch(HttpServletRequest request, HttpServletResponse response)
throws GadgetException, IOException {
HttpRequest rcr = buildHttpRequest(request);
String container = rcr.getContainer();
final Uri gadgetUri = rcr.getGadget();
if (gadgetUri == null) {
throw new GadgetException(GadgetException.Code.MISSING_PARAMETER,
"Unable to find gadget in request", HttpResponse.SC_FORBIDDEN);
}
Gadget gadget;
GadgetContext context = new HttpGadgetContext(request) {
@Override
public Uri getUrl() {
return gadgetUri;
}
@Override
public boolean getIgnoreCache() {
return getParameter("bypassSpecCache").equals("1");
}
};
try {
gadget = processor.process(context);
} catch (ProcessingException e) {
throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR,
"Error processing gadget", e, HttpResponse.SC_BAD_REQUEST);
}
// Validate gadget is correct for the host.
// Ensures that the gadget has not hand crafted this request to represent itself as
// another gadget in a locked domain environment.
if (!lockedDomainService.isGadgetValidForHost(context.getHost(), gadget, container)) {
throw new GadgetException(GadgetException.Code.GADGET_HOST_MISMATCH,
"The gadget is incorrect for this request", HttpResponse.SC_FORBIDDEN);
}
if (!gadgetAdminStore.isWhitelisted(container, gadgetUri.toString())) {
throw new GadgetException(GadgetException.Code.NON_WHITELISTED_GADGET,
"The requested content is unavailable", HttpResponse.SC_FORBIDDEN);
}
// Serialize the response