protected final FeatureRegistry getFeatureRegistry(JsUri jsUri) throws JsException { try { return featureRegistryProvider.get(jsUri.getRepository()); } catch (GadgetException e) { throw new JsException(e.getHttpStatusCode(), e.getMessage()); } }
@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(); }
public boolean process(JsRequest request, JsResponseBuilder builder) throws JsException { JsUri jsUri = request.getJsUri(); // Add onload handler to add callback function. String onloadStr = jsUri.getOnload(); if (onloadStr != null) { if (!ONLOAD_FN_PATTERN.matcher(onloadStr).matches()) { throw new JsException(HttpServletResponse.SC_BAD_REQUEST, ONLOAD_FUNCTION_NAME_ERROR); } builder.appendJs(createOnloadScript(onloadStr), ONLOAD_CODE_ID); } else if (jsUri.isNohint()) { // "Second-stage" JS, which may have had a callback set by loader. // This type of JS doesn't create a hint, but does attempt to use one. builder.appendJs(JSL_CALLBACK_JS, JSL_CODE_ID, true); } return true; }
@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 JsResponse execute(JsRequest jsRequest) throws JsException { JsResponseBuilder resp = new JsResponseBuilder(); jsProcessorRegistry.process(jsRequest, resp); final JsResponse response = resp.build(); if (response.isError()) { throw new JsException(response.getStatusCode(), response.toErrorString()); } return response; } }
@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(); }
public boolean process(JsRequest request, JsResponseBuilder builder) throws JsException { JsUri jsUri = request.getJsUri(); // Don't emit the JS itself; instead emit JS loader script that loads // versioned JS. The loader script is much smaller and cacheable for a // configurable amount of time. if (jsUri.isJsload()) { // Require users to specify &onload=. This ensures a reliable continuation // of JS execution. IE asynchronously loads script, before it loads // source-scripted and inlined JS. if (requireOnload && jsUri.getOnload() == null) { throw new JsException(HttpServletResponse.SC_BAD_REQUEST, JSLOAD_ONLOAD_ERROR); } int refresh = getCacheTtlSecs(jsUri); builder.setCacheTtlSecs(refresh); builder.setProxyCacheable(true); doJsload(jsUri, builder); return false; } return true; }
@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 testJsFailure() throws Exception { registerGadgetsHandler(null); List<String> features = ImmutableList.of("rpc2"); List<String> loadedFeatures = ImmutableList.of(); Uri jsUri = Uri.parse("http://shindig.com/gadgets/js/rpc:io"); String onload = "do \"this\";"; JSONObject request = makeComplexJsRequest(features, loadedFeatures, onload, null); Capture<JsUri> captureUri = new Capture<JsUri>(); EasyMock.expect(jsUriManager.makeExternJsUri(EasyMock.capture(captureUri))).andReturn(jsUri); EasyMock.expect(jsPipeline.execute(EasyMock.isA(JsRequest.class))).andThrow( new JsException(404, "not found")); replay(); RpcHandler operation = registry.getRpcHandler(request); Object responseObj = operation.execute(emptyFormItems, authContext, converter).get(); JSONObject results = new JSONObject(converter.convertToString(responseObj)); assertFalse(results.has("jsUrl")); assertEquals(HttpResponse.SC_NOT_FOUND, results.getJSONObject("error").getInt("code")); assertTrue(results.getJSONObject("error").getString("message").contains("not found")); 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); }
@Test public void testJsFailure() throws Exception { registerGadgetsHandler(null); List<String> features = ImmutableList.of("rpc2"); List<String> loadedFeatures = ImmutableList.of(); Uri jsUri = Uri.parse("http://shindig.com/gadgets/js/rpc:io"); String onload = "do \"this\";"; JSONObject request = makeComplexJsRequest(features, loadedFeatures, onload, null); Capture<JsUri> captureUri = new Capture<JsUri>(); EasyMock.expect(jsUriManager.makeExternJsUri(EasyMock.capture(captureUri))).andReturn(jsUri); EasyMock.expect(jsPipeline.execute(EasyMock.isA(JsRequest.class))).andThrow( new JsException(404, "not found")); replay(); RpcHandler operation = registry.getRpcHandler(request); Object responseObj = operation.execute(emptyFormItems, authContext, converter).get(); JSONObject results = new JSONObject(converter.convertToString(responseObj)); assertFalse(results.has("jsUrl")); assertEquals(HttpResponse.SC_NOT_FOUND, results.getJSONObject("error").getInt("code")); assertTrue(results.getJSONObject("error").getString("message").contains("not found")); verify(); }
@Test(expected = ProcessingException.class) public void testGetJsDataWithException() throws Exception { List<String> fields = ImmutableList.of("jscontent"); List<String> features = ImmutableList.of("unknown"); Uri resUri = Uri.parse("http://server.com/gadgets/js/foo"); Capture<JsUri> uriCapture = new Capture<JsUri>(); expect(jsUriManager.makeExternJsUri(capture(uriCapture))).andReturn(resUri); expect(jsPipeline.execute(EasyMock.isA(JsRequest.class))).andThrow( new JsException(404, "error")); GadgetsHandlerApi.JsRequest request = createJsRequest(FakeProcessor.SPEC_URL.toString(), CONTAINER, fields, features, null); expect(request.getOnload()).andStubReturn("do this"); expect(request.getContext()).andStubReturn(GadgetsHandlerApi.RenderingContext.CONTAINER); replay(); gadgetHandler.getJs(request); }
public boolean process(JsRequest request, JsResponseBuilder builder) throws JsException { JsUri jsUri = request.getJsUri(); GadgetContext ctx = new JsGadgetContext(jsUri); FeatureRegistry registry; try { registry = registryProvider.get(jsUri.getRepository()); } catch (GadgetException e) { throw new JsException(e.getHttpStatusCode(), e.getMessage()); } // Append gadgets.config initialization if not in standard gadget mode. if (ctx.getRenderingContext() != RenderingContext.GADGET) { List<String> allReq = registry.getFeatures(jsUri.getLibs()); Collection<String> loaded = jsUri.getLoadedLibs(); // Only inject config for features not already present and configured. List<String> newReq = subtractCollection(allReq, loaded); Map<String, Object> config = configProcessor.getConfig( ctx.getContainer(), newReq, request.getHost(), null); if (!config.isEmpty()) { String configJson = JsonSerializer.serialize(config); if (allReq.contains(CONFIG_FEATURE) || loaded.contains(CONFIG_FEATURE)) { // config lib is present: pass it data injectBaseConfig(configJson, builder); } else { // config lib not available: use global variable injectGlobalConfig(configJson, builder); } } } return true; }
@Test(expected = ProcessingException.class) public void testGetJsDataWithException() throws Exception { List<String> fields = ImmutableList.of("jscontent"); List<String> features = ImmutableList.of("unknown"); Uri resUri = Uri.parse("http://server.com/gadgets/js/foo"); Capture<JsUri> uriCapture = new Capture<JsUri>(); expect(jsUriManager.makeExternJsUri(capture(uriCapture))).andReturn(resUri); expect(jsPipeline.execute(EasyMock.isA(JsRequest.class))).andThrow( new JsException(404, "error")); GadgetsHandlerApi.JsRequest request = createJsRequest(FakeProcessor.SPEC_URL.toString(), CONTAINER, fields, features, null); expect(request.getOnload()).andStubReturn("do this"); expect(request.getContext()).andStubReturn(GadgetsHandlerApi.RenderingContext.CONTAINER); replay(); gadgetHandler.getJs(request); }
registry = registryProvider.get(jsUri.getRepository()); } catch (GadgetException e) { throw new JsException(e.getHttpStatusCode(), e.getMessage());