@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException { JsRequest jsRequest; try { jsRequest = jsRequestBuilder.build(req); } catch (GadgetException e) { resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); return; } JsResponse jsResponse; try { jsResponse = jsServingPipeline.execute(jsRequest); } catch (JsException e) { resp.sendError(e.getStatusCode(), e.getMessage()); return; } emitJsResponse(jsResponse, req, resp); }
/** * Get the JS content for a request (JsUri) */ protected String getFeaturesContent(JsUri jsUri) throws GadgetException { // Inject js content, fetched from JsPipeline JsRequest jsRequest = new JsRequestBuilder(jsUriManager, featureRegistryProvider.get(jsUri.getRepository())).build(jsUri, null); JsResponse jsResponse; try { jsResponse = jsServingPipeline.execute(jsRequest); } catch (JsException e) { throw new GadgetException(Code.JS_PROCESSING_ERROR, e, e.getStatusCode()); } return jsResponse.toJsString(); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
@Test public void testFailsWhenNoOnloadIsSpecified() throws Exception { EasyMock.expect(jsUri.isJsload()).andReturn(true); EasyMock.expect(jsUri.getOnload()).andReturn(null); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(JsLoadProcessor.JSLOAD_ONLOAD_ERROR, e.getMessage()); } control.verify(); }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }
@Test public void testFailsWithInvalidFunctionName() throws Exception { EasyMock.expect(jsUri.getOnload()).andReturn("!!%%!!%%"); control.replay(); try { processor.process(request, response); fail("A JsException should have been thrown by the processor."); } catch (JsException e) { assertEquals(HttpServletResponse.SC_BAD_REQUEST, e.getStatusCode()); assertEquals(AddOnloadFunctionProcessor.ONLOAD_FUNCTION_NAME_ERROR, e.getMessage()); } control.verify(); }
public GadgetsHandlerApi.JsResponse getJs(GadgetsHandlerApi.JsRequest request) throws ProcessingException { verifyBaseParams(request, false); Set<String> fields = beanFilter.processBeanFields(request.getFields()); JsUri jsUri = createJsUri(request); Uri servedUri = jsUriManager.makeExternJsUri(jsUri); String content = null; Long expireMs = null; if (isFieldIncluded(fields, "jsContent")) { JsResponse response; try { response = jsPipeline.execute(jsRequestBuilder.build(jsUri, servedUri.getAuthority())); } catch (JsException e) { throw new ProcessingException(e.getMessage(), e.getStatusCode()); } content = response.toJsString(); if (response.isProxyCacheable()) { expireMs = getDefaultExpiration(); } } else { expireMs = getDefaultExpiration(); } return createJsResponse(request.getUrl(), servedUri, content, fields, expireMs); }