/** * Render the gadget into a string by performing the following steps: * * - Retrieve gadget specification information (GadgetSpec, MessageBundle, etc.) * * - Fetch any preloaded data needed to handle the request, as handled by Preloader. * * - Perform rewriting operations on the output content, handled by Rewriter. * * @param gadget The gadget for the rendering operation. * @return The rendered gadget content * @throws RenderingException if any issues arise that prevent rendering. */ public String render(Gadget gadget) { try { View view = gadget.getCurrentView(); GadgetContext context = gadget.getContext(); GadgetSpec spec = gadget.getSpec(); IPreloads preloads = preloader.preload(context, spec, PreloaderService.PreloadPhase.HTML_RENDER); gadget.setPreloads(preloads); String content; if (view.getHref() == null) { content = view.getContent(); } else { // TODO: Add current url to GadgetContext to support transitive proxying. UriBuilder uri = new UriBuilder(view.getHref()); uri.addQueryParameter("lang", context.getLocale().getLanguage()); uri.addQueryParameter("country", context.getLocale().getCountry()); sRequest request = new sRequest(uri.toUri()) .setIgnoreCache(context.getIgnoreCache()) .setOAuthArguments(new OAuthArguments(view)) .setAuthType(view.getAuthType()) .setSecurityToken(context.getToken()) .setContainer(context.getContainer()) .setGadget(spec.getUrl()); sResponse response = DefaultHttpCache.Instance.getResponse(request); if (response == null || response.isStale()) { sRequest proxyRequest = createPipelinedProxyRequest(gadget, request); response = requestPipeline.execute(proxyRequest); DefaultHttpCache.Instance.addResponse(request, response); } if (response.isError()) { throw new RenderingException("Unable to reach remote host. HTTP status " + response.getHttpStatusCode()); } content = response.responseString; } return(rewriter.rewriteGadget(gadget, content)); } catch (GadgetException e) { throw new RenderingException(e.Message, e); } }
/** * Injects preloads into the gadget output. * * If preloading fails for any reason, we just output an empty object. */ private static void InjectPreloads(Gadget gadget, Node scriptTag) { IPreloads preloads = gadget.getPreloads(); Dictionary <String, Object> preload = new Dictionary <string, object>(); foreach (PreloadedData preloaded in preloads.getData()) { foreach (var entry in preloaded.toJson()) { preload.Add(entry.Key, entry.Value); } } Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.io.preloaded_="); text.appendData(JsonConvert.ExportToString(preload)); text.appendData(";"); scriptTag.appendChild(text); }
/** * Creates a proxy request by fetching pipelined data and adding it to an existing request. * */ private sRequest createPipelinedProxyRequest(Gadget gadget, sRequest original) { sRequest request = new sRequest(original); request.setIgnoreCache(true); GadgetSpec spec = gadget.getSpec(); GadgetContext context = gadget.getContext(); IPreloads proxyPreloads = preloader.preload(context, spec, PreloaderService.PreloadPhase.PROXY_FETCH); // TODO: Add current url to GadgetContext to support transitive proxying. // POST any preloaded content if ((proxyPreloads != null) && proxyPreloads.getData().Count != 0) { JsonArray array = new JsonArray(); foreach (PreloadedData preload in proxyPreloads.getData()) { Dictionary <String, Object> dataMap = preload.toJson(); foreach (var entry in dataMap) { // TODO: the existing, supported content is JSONObjects that contain the // key already. Discarding the key is odd. array.Put(entry.Value); } } String postContent = array.ToString(); // POST the preloaded content, with a method override of GET // to enable caching request.setMethod("POST") .setPostBody(Encoding.UTF8.GetBytes(postContent)) .setHeader("Content-Type", "text/json;charset=utf-8"); } return(request); }
/** * @param preloads The preloads for the gadget that is being processed. */ public Gadget setPreloads(IPreloads preloads) { this.preloads = preloads; return this; }
/** * @param preloads The preloads for the gadget that is being processed. */ public Gadget setPreloads(IPreloads preloads) { this.preloads = preloads; return(this); }