Examples of GadgetContext


Examples of org.apache.shindig.gadgets.GadgetContext

  }

  public void rewrite(Gadget gadget, MutableContent mc) {
    if (!gadget.requiresCaja()) return;

    GadgetContext gadgetContext = gadget.getContext();
    boolean debug = gadgetContext.getDebug();
    Document doc = mc.getDocument();

    // Serialize outside of MutableContent, to prevent a re-parse.
    String docContent = HtmlSerialization.serialize(doc);
    Node root = doc.createDocumentFragment();
    root.appendChild(doc.getDocumentElement());

    if (debug) {
      gadget.addFeature("caja-debug");
    }

    InputSource is = new InputSource(gadgetContext.getUrl().toJavaUri());
    CajoledResult result =
      rewrite(gadgetContext.getUrl(), gadgetContext.getContainer(),
          new Dom(root), true, debug);

    if (result.hasErrors) {
      // Content is only used to produce useful snippets with error messages
      List<Message> messages = result.messages;
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

    // Process all JSON first so that we don't wind up with hanging threads if
    // a JSONException is thrown.
    gadgets = Lists.newArrayListWithExpectedSize(requestedGadgets.length());
   
    for (int i = 0, j = requestedGadgets.length(); i < j; ++i) {
      GadgetContext context = new JsonRpcGadgetContext(
          requestContext, requestedGadgets.getJSONObject(i));
      gadgets.add(context);
    }

    // Dispatch a separate thread for each gadget that we wish to render.
    // We could probably just submit these directly to the ExecutorService, but if it's an async
    // service instead of a threaded one we would just block.
    CompletionService<JSONObject> processor =  new ExecutorCompletionService<JSONObject>(executor);

    for (GadgetContext context : gadgets) {
      processor.submit(new Job(context));
    }

    JSONObject response = new JSONObject();

    int numJobs = gadgets.size();
    do {
      try {
        JSONObject gadget = processor.take().get();
        response.append("gadgets", gadget);
      } catch (InterruptedException e) {
        throw new RpcException("Processing interrupted", e);
      } catch (ExecutionException ee) {
        if (!(ee.getCause() instanceof RpcException)) {
          throw new RpcException("Processing interrupted", ee);
        }
        RpcException e = (RpcException)ee.getCause();
        // Just one gadget failed; mark it as such.
        try {
          GadgetContext context = e.getContext();
          JSONObject errorObj = new JSONObject();
          errorObj.put("url", context.getUrl())
                  .put("moduleId", context.getModuleId());
          errorObj.append("errors", e.getCause().getLocalizedMessage());
          response.append("gadgets", errorObj);
        } catch (JSONException je) {
          throw new RpcException("Unable to write JSON", je);
        }
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

  }

  public Collection<Callable<PreloadedData>> createPreloadTasks(Gadget gadget) {
    List<Callable<PreloadedData>> preloads = Lists.newArrayList();

    GadgetContext context = gadget.getContext();

    for (Preload preload : gadget.getSpec().getModulePrefs().getPreloads()) {
      Set<String> preloadViews = preload.getViews();
      if (preloadViews.isEmpty() || preloadViews.contains(context.getView())) {
        preloads.add(new PreloadTask(context, preload, preload.getHref().toString()));
      }
    }

    return preloads;
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

  public ContentRewriterFeature get(HttpRequest request) {
    final Uri gadgetUri = request.getGadget();
    GadgetSpec spec;
    if (gadgetUri != null) {
      try {
        GadgetContext context = new GadgetContext() {
          @Override
          public Uri getUrl() {
            return gadgetUri;
          }
        };
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

  }

  private GadgetSpec findSpec(final SecurityToken securityToken, final OAuthArguments arguments,
      OAuthResponseParams responseParams) throws OAuthRequestException {
    try {
      GadgetContext context = new OAuthGadgetContext(securityToken, arguments);
      return specFactory.getGadgetSpec(context);
    } catch (IllegalArgumentException e) {
      throw responseParams.oauthRequestException(OAuthError.UNKNOWN_PROBLEM,
          "Could not fetch gadget spec, gadget URI invalid.", e);
    } catch (GadgetException e) {
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

      throw new RuntimeException(e);
    }
  }

  private void injectBaseTag(Gadget gadget, Node headTag) {
    GadgetContext context = gadget.getContext();
    if (containerConfig.getBool(context.getContainer(), INSERT_BASE_ELEMENT_KEY)) {
      Uri base = gadget.getSpec().getUrl();
      View view = gadget.getCurrentView();
      if (view != null && view.getHref() != null) {
        base = view.getHref();
      }
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

   * Injects javascript libraries needed to satisfy feature dependencies.
   */
  private void injectFeatureLibraries(Gadget gadget, Node headTag) throws GadgetException {
    // TODO: If there isn't any js in the document, we can skip this. Unfortunately, that means
    // both script tags (easy to detect) and event handlers (much more complex).
    GadgetContext context = gadget.getContext();
    String forcedLibs = context.getParameter("libs");

    // List of forced libraries we need
    Set<String> forced;

    // gather the libraries we'll need to generate the forced libs
    if (forcedLibs == null || forcedLibs.length() == 0) {
      // Don't bother making a mutable copy if the list is empty
      forced = (defaultForcedLibs.isEmpty()) ? defaultForcedLibs :  Sets.newTreeSet(defaultForcedLibs);
    } else {
      forced = Sets.newTreeSet(Arrays.asList(forcedLibs.split(":")));
    }
    if (!forced.isEmpty()) {
      String jsUrl = urlGenerator.getBundledJsUrl(forced, context);
      Element libsTag = headTag.getOwnerDocument().createElement("script");
      libsTag.setAttribute("src", jsUrl);
      headTag.appendChild(libsTag);

      // Forced transitive deps need to be added as well so that they don't get pulled in twice.
      // Without this, a shared dependency between forced and non-forced libs would get pulled into
      // both the external forced script and the inlined script.
      // TODO: Figure out a clean way to avoid having to call getFeatures twice.
      for (GadgetFeature dep : featureRegistry.getFeatures(forced)) {
        forced.add(dep.getName());
      }
    }
    // Make this read-only
    forced = ImmutableSet.copyOf(forced);

    // Inline any libs that weren't forced. The ugly context switch between inline and external
    // Js is needed to allow both inline and external scripts declared in feature.xml.
    String container = context.getContainer();
    Collection<GadgetFeature> features = getFeatures(gadget, forced);

    // Precalculate the maximum length in order to avoid excessive garbage generation.
    int size = 0;
    for (GadgetFeature feature : features) {
      for (JsLibrary library : feature.getJsLibraries(RenderingContext.GADGET, container)) {
        if (library.getType().equals(JsLibrary.Type.URL)) {
          size += library.getContent().length();
        }
      }
    }

    // Really inexact.
    StringBuilder inlineJs = new StringBuilder(size);

    for (GadgetFeature feature : features) {
      for (JsLibrary library : feature.getJsLibraries(RenderingContext.GADGET, container)) {
        if (library.getType().equals(JsLibrary.Type.URL)) {
          if (inlineJs.length() > 0) {
            Element inlineTag = headTag.getOwnerDocument().createElement("script");
            headTag.appendChild(inlineTag);
            inlineTag.appendChild(headTag.getOwnerDocument().createTextNode(inlineJs.toString()));
            inlineJs.setLength(0);
          }
          Element referenceTag = headTag.getOwnerDocument().createElement("script");
          referenceTag.setAttribute("src", library.getContent());
          headTag.appendChild(referenceTag);
        } else {
          if (!forced.contains(feature.getName())) {
            // already pulled this file in from the shared contents.
            if (context.getDebug()) {
              inlineJs.append(library.getDebugContent());
            } else {
              inlineJs.append(library.getContent());
            }
            inlineJs.append(";\n");
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

   * @param reqs The features needed to satisfy the request.
   * @throws GadgetException If there is a problem with the gadget auth token
   */
  private String getLibraryConfig(Gadget gadget, Collection<GadgetFeature> reqs)
      throws GadgetException {
    GadgetContext context = gadget.getContext();

    Map<String, Object> features = containerConfig.getMap(context.getContainer(), FEATURES_KEY);

    Map<String, Object> config
        = Maps.newHashMapWithExpectedSize(features == null ? 2 : features.size() + 2);

    if (features != null) {
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

  /**
   * Injects message bundles into the gadget output.
   * @throws GadgetException If we are unable to retrieve the message bundle.
   */
  private void injectMessageBundles(Gadget gadget, Node scriptTag) throws GadgetException {
    GadgetContext context = gadget.getContext();
    MessageBundle bundle = messageBundleFactory.getBundle(
        gadget.getSpec(), context.getLocale(), context.getIgnoreCache());

    String msgs = bundle.toJSONString();

    Text text = scriptTag.getOwnerDocument().createTextNode("gadgets.Prefs.setMessages_(");
    text.appendData(msgs);
View Full Code Here

Examples of org.apache.shindig.gadgets.GadgetContext

    }

    resp.setContentType("text/html");
    resp.setCharacterEncoding("UTF-8");

    GadgetContext context = new HttpGadgetContext(req);
    RenderingResults results = renderer.render(context);
    switch (results.getStatus()) {
      case OK:
        if (context.getIgnoreCache()) {
          HttpUtil.setCachingHeaders(resp, 0);
        } else if (req.getParameter("v") != null) {
          // Versioned files get cached indefinitely
          HttpUtil.setCachingHeaders(resp, true);
        } else {
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.