예제 #1
0
        /// <summary>
        /// Process a single gadget. Creates a gadget from a retrieved GadgetSpec and context object,
        /// automatically performing variable substitution on the spec for use elsewhere.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Gadget Process(GadgetContext context)
        {
            Uri url = context.getUrl();

            if (url == null)
            {
                throw new ProcessingException("Missing or malformed url parameter");
            }

            if (!url.Scheme.ToLower().Equals("http") && !url.Scheme.ToLower().Equals("https"))
            {
                throw new ProcessingException("Unsupported scheme (must be http or https).");
            }

            if (blacklist.isBlacklisted(context.getUrl()))
            {
                throw new ProcessingException("The requested gadget is unavailable");
            }

            try
            {
                GadgetSpec spec = gadgetSpecFactory.getGadgetSpec(context);
                spec = substituter.substitute(context, spec);

                return(new Gadget()
                       .setContext(context)
                       .setSpec(spec)
                       .setCurrentView(GetView(context, spec)));
            }
            catch (GadgetException e)
            {
                throw new ProcessingException(e.Message, e);
            }
        }
예제 #2
0
파일: Processor.cs 프로젝트: s7loves/pesta
        /// <summary>
        /// Process a single gadget. Creates a gadget from a retrieved GadgetSpec and context object,
        /// automatically performing variable substitution on the spec for use elsewhere.
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public Gadget Process(GadgetContext context)
        {
            Uri url = context.getUrl();

            if (url == null)
            {
                throw new ProcessingException("Missing or malformed url parameter");
            }

            if (!url.Scheme.ToLower().Equals("http") && !url.Scheme.ToLower().Equals("https"))
            {
                throw new ProcessingException("Unsupported scheme (must be http or https).");
            }

            if (blacklist.isBlacklisted(context.getUrl()))
            {
                throw new ProcessingException("The requested gadget is unavailable");
            }

            try
            {
                GadgetSpec spec = gadgetSpecFactory.getGadgetSpec(context);
                spec = substituter.substitute(context, spec);

                return new Gadget()
                    .setContext(context)
                    .setSpec(spec)
                    .setCurrentView(GetView(context, spec));
            } 
            catch (GadgetException e) 
            {
                throw new ProcessingException(e.Message, e);
            }
        }
예제 #3
0
        /**
         * Attempts to render the requested gadget.
         *
         * @return The results of the rendering attempt.
         *
         * TODO: Localize error messages.
         */
        public RenderingResults Render(GadgetContext context)
        {
            if (!ValidateParent(context))
            {
                return(RenderingResults.error("Unsupported parent parameter. Check your container code."));
            }

            try
            {
                Gadget gadget = processor.Process(context);

                if (gadget.getCurrentView() == null)
                {
                    return(RenderingResults.error("Unable to locate an appropriate view in this gadget. " +
                                                  "Requested: '" + gadget.getContext().getView() +
                                                  "' Available: " + String.Join(",", gadget.getSpec().getViews().Keys.ToArray())));
                }

                if (gadget.getCurrentView().getType() == View.ContentType.URL)
                {
                    return(RenderingResults.mustRedirect(getRedirect(gadget)));
                }

                GadgetSpec spec = gadget.getSpec();
                if (!lockedDomainService.gadgetCanRender(context.getHost(), spec, context.getContainer()))
                {
                    return(RenderingResults.mustRedirect(getRedirect(gadget)));
                }
                return(RenderingResults.ok(renderer.render(gadget)));
            }
            catch (RenderingException e)
            {
                return(LogError(context.getUrl(), e));
            }
            catch (ProcessingException e)
            {
                return(LogError(context.getUrl(), e));
            }
            catch (Exception e)
            {
                if (e.GetBaseException() is GadgetException)
                {
                    return(LogError(context.getUrl(), e.GetBaseException()));
                }
                throw;
            }
        }
예제 #4
0
        /**
         * Processes a JSON request.
         *
         * @param request Original JSON request
         * @return The JSON response.
         */
        public JsonObject Process(JsonObject request)
        {
            JsonObject response = new JsonObject();

            JsonObject requestContext   = request.getJSONObject("context");
            JsonArray  requestedGadgets = request["gadgets"] as JsonArray;

            // Process all JSON first so that we don't wind up with hanging threads if
            // a JsonException is thrown.
            List <IAsyncResult> gadgets = new List <IAsyncResult>(requestedGadgets.Length);

            for (int i = 0, j = requestedGadgets.Length; i < j; ++i)
            {
                var context             = new JsonRpcGadgetContext(requestContext, (JsonObject)requestedGadgets[i]);
                PreloadProcessor proc   = new PreloadProcessor(CallJob);
                IAsyncResult     result = proc.BeginInvoke(context, null, null);
                gadgets.Add(result);
            }

            foreach (var entry in gadgets)
            {
                try
                {
                    AsyncResult      result = (AsyncResult)entry;
                    PreloadProcessor proc   = (PreloadProcessor)result.AsyncDelegate;
                    JsonObject       gadget = proc.EndInvoke(result);
                    response.Accumulate("gadgets", gadget);
                }
                catch (JsonException e)
                {
                    throw new RpcException("Unable to write JSON", e);
                }
                catch (Exception ee)
                {
                    if (!(ee is RpcException))
                    {
                        throw new RpcException("Processing interrupted", ee);
                    }
                    RpcException e = (RpcException)ee;
                    // Just one gadget failed; mark it as such.
                    try
                    {
                        GadgetContext context = e.Context;

                        JsonObject errorObj = new JsonObject();
                        errorObj.Put("url", context.getUrl())
                        .Put("moduleId", context.getModuleId());
                        errorObj.Accumulate("errors", e.Message);
                        response.Accumulate("gadgets", errorObj);
                    }
                    catch (JsonException je)
                    {
                        throw new RpcException("Unable to write JSON", je);
                    }
                }
            }
            return(response);
        }
예제 #5
0
 public static sRequest newHttpRequest(GadgetContext context,
             RequestAuthenticationInfo authenticationInfo)
 {
     sRequest request = new sRequest(authenticationInfo.getHref())
         .setSecurityToken(context.getToken())
         .setOAuthArguments(new OAuthArguments(authenticationInfo))
         .setAuthType(authenticationInfo.getAuthType())
         .setContainer(context.getContainer())
         .setGadget(Uri.fromJavaUri(context.getUrl()));
     return request;
 }
예제 #6
0
        public static sRequest newHttpRequest(GadgetContext context,
                                              RequestAuthenticationInfo authenticationInfo)
        {
            sRequest request = new sRequest(authenticationInfo.getHref())
                               .setSecurityToken(context.getToken())
                               .setOAuthArguments(new OAuthArguments(authenticationInfo))
                               .setAuthType(authenticationInfo.getAuthType())
                               .setContainer(context.getContainer())
                               .setGadget(Uri.fromJavaUri(context.getUrl()));

            return(request);
        }
예제 #7
0
파일: Renderer.cs 프로젝트: s7loves/pesta
        /**
        * Attempts to render the requested gadget.
        *
        * @return The results of the rendering attempt.
        *
        * TODO: Localize error messages.
        */
        public RenderingResults Render(GadgetContext context) 
        {
            if (!ValidateParent(context)) 
            {
                return RenderingResults.error("Unsupported parent parameter. Check your container code.");
            }

            try 
            {
                Gadget gadget = processor.Process(context);

                if (gadget.getCurrentView() == null)
                {
                    return RenderingResults.error("Unable to locate an appropriate view in this gadget. " +
                                                  "Requested: '" + gadget.getContext().getView() +
                                                  "' Available: " + String.Join(",",gadget.getSpec().getViews().Keys.ToArray()));
                }

                if (gadget.getCurrentView().getType() == View.ContentType.URL)
                {
                    return RenderingResults.mustRedirect(getRedirect(gadget));
                }

                GadgetSpec spec = gadget.getSpec();
                if (!lockedDomainService.gadgetCanRender(context.getHost(), spec, context.getContainer()))
                {
                    return RenderingResults.mustRedirect(getRedirect(gadget));
                }
                return RenderingResults.ok(renderer.render(gadget));
            }
            catch (RenderingException e) 
            {
                return LogError(context.getUrl(), e);
            } 
            catch (ProcessingException e) 
            {
                return LogError(context.getUrl(), e);
            } 
            catch (Exception e) 
            {
                if (e.GetBaseException() is GadgetException) 
                {
                    return LogError(context.getUrl(), e.GetBaseException());
                }
                throw;
            }
        }
예제 #8
0
        private JsonObject CallJob(GadgetContext context)
        {
            try
            {
                JsonObject gadgetJson = new JsonObject();
                Gadget gadget = Processor.Process(context);

                GadgetSpec spec = gadget.getSpec();
                ModulePrefs prefs = spec.getModulePrefs();

                // TODO: modularize response fields based on requested items.
                JsonObject views = new JsonObject();
                foreach (View view in spec.getViews().Values)
                {
                    views.Put(view.getName(), new JsonObject()
                                                  // .Put("content", view.getContent())
                                                  .Put("type", view.getType().ToString().ToLower())
                                                  .Put("quirks", view.getQuirks())
                                                  .Put("preferredHeight", view.getPreferredHeight())
                                                  .Put("preferredWidth", view.getPreferredWidth()));
                }

                // Features.
                List<String> feats = new List<String>();
                foreach (var entry in prefs.getFeatures())
                {
                    feats.Add(entry.Key);
                }
                string[] features = new string[feats.Count];
                feats.CopyTo(features, 0);

                // Links
                JsonObject links = new JsonObject();
                foreach (LinkSpec link in prefs.getLinks().Values)
                {
                    links.Put(link.getRel(), link.getHref());
                }

                JsonObject userPrefs = new JsonObject();

                // User pref specs
                foreach (UserPref pref in spec.getUserPrefs())
                {
                    JsonObject up = new JsonObject()
                        .Put("displayName", pref.getDisplayName())
                        .Put("type", pref.getDataType().ToString().ToLower())
                        .Put("default", pref.getDefaultValue())
                        .Put("enumValues", pref.getEnumValues())
                        .Put("orderedEnumValues", getOrderedEnums(pref));
                    userPrefs.Put(pref.getName(), up);
                }

                // TODO: This should probably just copy all data from
                // ModulePrefs.getAttributes(), but names have to be converted to
                // camel case.
                gadgetJson.Put("iframeUrl", UrlGenerator.getIframeUrl(gadget))
                    .Put("url", context.getUrl().ToString())
                    .Put("moduleId", context.getModuleId())
                    .Put("title", prefs.getTitle())
                    .Put("titleUrl", prefs.getTitleUrl().ToString())
                    .Put("views", views)
                    .Put("features", features)
                    .Put("userPrefs", userPrefs)
                    .Put("links", links)

                    // extended meta data
                    .Put("directoryTitle", prefs.getDirectoryTitle())
                    .Put("description", prefs.getDescription())
                    .Put("thumbnail", prefs.getThumbnail().ToString())
                    .Put("screenshot", prefs.getScreenshot().ToString())
                    .Put("author", prefs.getAuthor())
                    .Put("authorEmail", prefs.getAuthorEmail())
                    .Put("authorAffiliation", prefs.getAuthorAffiliation())
                    .Put("authorLocation", prefs.getAuthorLocation())
                    .Put("authorPhoto", prefs.getAuthorPhoto())
                    .Put("authorAboutme", prefs.getAuthorAboutme())
                    .Put("authorQuote", prefs.getAuthorQuote())
                    .Put("authorLink", prefs.getAuthorLink())
                    .Put("categories", prefs.getCategories())
                    .Put("screenshot", prefs.getScreenshot().ToString())
                    .Put("height", prefs.getHeight())
                    .Put("width", prefs.getWidth())
                    .Put("showStats", prefs.getShowStats())
                    .Put("showInDirectory", prefs.getShowInDirectory())
                    .Put("singleton", prefs.getSingleton())
                    .Put("scaling", prefs.getScaling())
                    .Put("scrolling", prefs.getScrolling());
                return gadgetJson;
            }
            catch (ProcessingException e)
            {
                throw new RpcException(context, e);
                //throw e;
            }
            catch (JsonException e)
            {
                // Shouldn't be possible
                throw new RpcException(context, e);
            }
        }
예제 #9
0
        private JsonObject CallJob(GadgetContext context)
        {
            try
            {
                JsonObject gadgetJson = new JsonObject();
                Gadget     gadget     = Processor.Process(context);

                GadgetSpec  spec  = gadget.getSpec();
                ModulePrefs prefs = spec.getModulePrefs();

                // TODO: modularize response fields based on requested items.
                JsonObject views = new JsonObject();
                foreach (View view in spec.getViews().Values)
                {
                    views.Put(view.getName(), new JsonObject()
                              // .Put("content", view.getContent())
                              .Put("type", view.getType().ToString().ToLower())
                              .Put("quirks", view.getQuirks())
                              .Put("preferredHeight", view.getPreferredHeight())
                              .Put("preferredWidth", view.getPreferredWidth()));
                }

                // Features.
                List <String> feats = new List <String>();
                foreach (var entry in prefs.getFeatures())
                {
                    feats.Add(entry.Key);
                }
                string[] features = new string[feats.Count];
                feats.CopyTo(features, 0);

                // Links
                JsonObject links = new JsonObject();
                foreach (LinkSpec link in prefs.getLinks().Values)
                {
                    links.Put(link.getRel(), link.getHref());
                }

                JsonObject userPrefs = new JsonObject();

                // User pref specs
                foreach (UserPref pref in spec.getUserPrefs())
                {
                    JsonObject up = new JsonObject()
                                    .Put("displayName", pref.getDisplayName())
                                    .Put("type", pref.getDataType().ToString().ToLower())
                                    .Put("default", pref.getDefaultValue())
                                    .Put("enumValues", pref.getEnumValues())
                                    .Put("orderedEnumValues", getOrderedEnums(pref));
                    userPrefs.Put(pref.getName(), up);
                }

                // TODO: This should probably just copy all data from
                // ModulePrefs.getAttributes(), but names have to be converted to
                // camel case.
                gadgetJson.Put("iframeUrl", UrlGenerator.getIframeUrl(gadget))
                .Put("url", context.getUrl().ToString())
                .Put("moduleId", context.getModuleId())
                .Put("title", prefs.getTitle())
                .Put("titleUrl", prefs.getTitleUrl().ToString())
                .Put("views", views)
                .Put("features", features)
                .Put("userPrefs", userPrefs)
                .Put("links", links)

                // extended meta data
                .Put("directoryTitle", prefs.getDirectoryTitle())
                .Put("description", prefs.getDescription())
                .Put("thumbnail", prefs.getThumbnail().ToString())
                .Put("screenshot", prefs.getScreenshot().ToString())
                .Put("author", prefs.getAuthor())
                .Put("authorEmail", prefs.getAuthorEmail())
                .Put("authorAffiliation", prefs.getAuthorAffiliation())
                .Put("authorLocation", prefs.getAuthorLocation())
                .Put("authorPhoto", prefs.getAuthorPhoto())
                .Put("authorAboutme", prefs.getAuthorAboutme())
                .Put("authorQuote", prefs.getAuthorQuote())
                .Put("authorLink", prefs.getAuthorLink())
                .Put("categories", prefs.getCategories())
                .Put("screenshot", prefs.getScreenshot().ToString())
                .Put("height", prefs.getHeight())
                .Put("width", prefs.getWidth())
                .Put("showStats", prefs.getShowStats())
                .Put("showInDirectory", prefs.getShowInDirectory())
                .Put("singleton", prefs.getSingleton())
                .Put("scaling", prefs.getScaling())
                .Put("scrolling", prefs.getScrolling());
                return(gadgetJson);
            }
            catch (ProcessingException e)
            {
                throw new RpcException(context, e);
                //throw e;
            }
            catch (JsonException e)
            {
                // Shouldn't be possible
                throw new RpcException(context, e);
            }
        }