protected final FeatureRegistry getFeatureRegistry(JsUri jsUri) throws JsException { try { return featureRegistryProvider.get(jsUri.getRepository()); } catch (GadgetException e) { throw new JsException(e.getHttpStatusCode(), e.getMessage()); } }
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; }
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; } }
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 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 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());