Package org.restlet.routing

Examples of org.restlet.routing.TemplateRoute


  private void attachResource(IResourceProvider resourceProvider)
  {
    for (String path : resourceProvider.getPaths())
    {
      TemplateRoute templateRoute = router.attach(path, resourceProvider.getInboundRoot(router.getContext()));
      templateRoute.setMatchingMode(resourceProvider.getMatchingMode());
    }
  }
View Full Code Here


     * @return the created route, or null.
     */
    @SuppressWarnings("unchecked")
    private TemplateRoute attach(Router router, String targetClassName,
            String uriPattern, boolean defaultRoute) {
        TemplateRoute route = null;

        // Load the application class using the given class name
        if (targetClassName != null) {
            try {
                final Class<?> targetClass = Engine.loadClass(targetClassName);
View Full Code Here

     *            Is this route the default one?
     * @return the created route, or null.
     */
    private TemplateRoute attachWithDescriptor(Router router,
            String targetDescriptor, String uriPattern, boolean defaultRoute) {
        TemplateRoute route = null;
        String targetClassName = null;

        try {
            // Only WADL descriptors are supported at this moment.
            targetClassName = "org.restlet.ext.wadl.WadlApplication";
View Full Code Here

                item = childNode.getAttributes().getNamedItem("default");
                boolean bDefault = getBoolean(item, false)
                        || "attachDefault".equals(childNode.getNodeName());

                // Attaches a new route.
                TemplateRoute route = null;
                item = childNode.getAttributes().getNamedItem("targetClass");

                if (item != null) {
                    route = attach(router, item.getNodeValue(), uriPattern,
                            bDefault);
                } else {
                    item = childNode.getAttributes().getNamedItem(
                            "targetDescriptor");
                    if (item != null) {
                        route = attachWithDescriptor(router,
                                item.getNodeValue(), uriPattern, bDefault);
                    } else {
                        getLogger()
                                .log(Level.WARNING,
                                        "Both targetClass name and targetDescriptor are missing. Couldn't attach a new route.");
                    }
                }

                if (route != null) {
                    Template template = route.getTemplate();
                    item = childNode.getAttributes().getNamedItem(
                            "matchingMode");
                    template.setMatchingMode(getInt(item,
                            router.getDefaultMatchingMode()));
                    item = childNode.getAttributes().getNamedItem(
                            "defaultVariableType");
                    template.getDefaultVariable().setType(
                            getInt(item, Variable.TYPE_URI_SEGMENT));

                    // Parse possible parameters specific to this AttachType
                    final NodeList childNodes2 = childNode.getChildNodes();
                    for (int j = 0; j < childNodes2.getLength(); j++) {
                        Node aNode = childNodes2.item(j);
                        if (isParameter(aNode)) {
                            Parameter p = parseParameter(aNode);
                            if (p != null) {
                                route.getNext().getContext().getParameters()
                                        .add(p);
                            }
                        }
                    }
                }
View Full Code Here

    }

    @Override
    protected TemplateRoute createRoute(String uriPattern, Restlet target,
            int matchingMode) {
        TemplateRoute result = new TemplateRoute(this, uriPattern, target) {
            @Override
            protected int beforeHandle(Request request, Response response) {
                final int result = super.beforeHandle(request, response);

                // Set the request's root reference in order to help the
                // retrieval of the relative reference.
                request.setRootRef(request.getResourceRef().getBaseRef());

                return result;
            }
        };

        result.getTemplate().setMatchingMode(matchingMode);
        result.setMatchingQuery(getDefaultMatchingQuery());
        return result;
    }
View Full Code Here

                  // reattach to make sure it is in the right order
                  router.detach(appInfo.app);
                  Term match = entry.getTerm(T_APP_MATCH);
                  if (match!=null && match.getValues()!=null) {
                     for (String pattern : match.getValues()) {
                        TemplateRoute route = router.attach(pattern,appInfo.app);
                        if (exact) {
                           route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                        }
                     }
                  } else {
                     TemplateRoute route = router.attach("",appInfo.app);
                     if (exact) {
                        route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                     }
                  }
                  getLogger().info("No changes, skipping.");
                  continue;
               }
               for (Term t : entry.getTerms().values()) {
                  getLogger().info(t.getURI()+": "+t.getFirstValue());
               }
               getLogger().info(T_APP_MATCH.toString());
               Term classTerm = entry.getTerm(T_APP_CLASS);
               Term proxyTerm = entry.getTerm(T_APP_PROXY);
               Context appContext = context.createChildContext();
               LinkSet set = new LinkSet();
               set.addLinkSet(entry.getLinks());
               set.addLinkSet(hostConf.getLinks());
               appContext.getAttributes().put(WebComponent.LINKS_ATTR,set);
               appContext.getAttributes().put(ScriptManager.ATTR,scriptManager);
               appContext.getAttributes().put(ResourceManager.ATTR,resourceManager);
               for (URI t : entry.getTerms().keySet()) {
                  String value = entry.getTerm(t).getFirstValue();
                  getLogger().info("Setting parameter: "+t+"="+value);
                  appContext.getParameters().set(t.toString(),value,false);
               }
               appContext.getParameters().set("username",autoConf.getUsername(),false);
               appContext.getParameters().set("password",autoConf.getPassword(),false);
               for (Term t : entry.getTerms().values()) {
                  String key = t.getURI().toString();
                  if (t.getValues()!=null) {
                     for (String value : t.getValues()) {
                        appContext.getParameters().add(key, value);
                     }
                  } else {
                     appContext.getParameters().add(key,"true");
                  }
               }
               Application app = null;
               if (proxyTerm!=null) {
                  String value = proxyTerm.getFirstValue();
                  List<Link> links = entry.getLinks().get(value);
                  Link target = null;
                  if (links!=null && links.size()>0) {
                     target = links.get(0);
                  }
                  if (target==null) {
                     links = hostConf.getLinks().get(value);
                     if (links!=null && links.size()>0) {
                        target = links.get(0);
                     }
                  }
                  if (target!=null) {
                     app = new ProxyApplication(appContext,target);
                  }
               } else if (classTerm!=null) {
                  String className = classTerm.getFirstValue();
                  List<Link> libraryLinks = entry.getLinks().get("library");
                  String href = null;
                  Text text = entry.getContent();
                  if (text!=null) {
                     href = text.getSourceLink();
                  }
                  Class<Application> appClass = null;
                  Class<?> foundClass = null;
                  if ((libraryLinks==null || libraryLinks.size()==0) && href==null) {
                     foundClass = Class.forName(className);
                  } else {
                     URL [] downloads = new URL[(libraryLinks==null ? 0 : libraryLinks.size())+(href==null ? 0 : 1)];
                     if (href!=null) {
                        downloads[0] = entry.getDocument().getDocumentElement().getBaseURI().resolve(href).toURL();
                     }
                     if (libraryLinks!=null) {
                        for (int pos = href==null ? 0 : 1; pos<libraryLinks.size(); pos++) {
                           Link link = libraryLinks.get(pos);
                           downloads[pos] = link.getLink().toURL();
                        }
                     }
                    
                     URL [] urls = new URL[downloads.length];
                     for (int i=0; i<downloads.length; i++) {
                        File jarfile = File.createTempFile("jar-T"+System.currentTimeMillis()+"-", ".jar");
                        urls[i] = jarfile.toURL();
                        URLRetriever retriever = new URLRetriever(downloads[i]);
                        try {
                           retriever.retrieve(jarfile, autoConf.getUsername(), autoConf.getPassword());
                        } catch (IOException ex) {
                           getLogger().info("Cannot download "+downloads[i]+" due to: "+ex.getMessage());
                           continue;
                        }
                     }
                     URLClassLoader classLoader = new URLClassLoader(urls,ConfiguredHost.class.getClassLoader()) {
                        protected PermissionCollection getPermissions(CodeSource source) {
                           PermissionCollection collection = super.getPermissions(source);
                           URL url = source.getLocation();
                           getLogger().info("Code source: "+url);
                           Enumeration<Permission> permissions = collection.elements();
                           while (permissions.hasMoreElements()) {
                              Permission p = permissions.nextElement();
                              getLogger().info("Permission: "+p.getName()+", "+p.getClass().getName()+", action: "+p.getActions());
                           }
                           return collection;
                        }
                     };
                     foundClass = classLoader.loadClass(className);
                     PermissionCollection collection = foundClass.getProtectionDomain().getPermissions();
                     Enumeration<Permission> permissions = collection.elements();
                     while (permissions.hasMoreElements()) {
                        Permission p = permissions.nextElement();
                        getLogger().info("Permission: "+p.getName()+", "+p.getClass().getName()+", action: "+p.getActions());
                     }
                  }
                  if (!Application.class.isAssignableFrom(foundClass)) {
                     getLogger().info("Class "+className+" is not an subclas of "+Application.class.getName());
                     continue;
                  }
                  appClass = (Class<Application>)foundClass;
                  Constructor<Application> makeit = appClass.getConstructor(Context.class);
                  app = makeit.newInstance(appContext);
               }
               if (app!=null) {
                  if (appInfo!=null) {
                     router.detach(appInfo.app);
                     appInfo.app = app;
                     appInfo.edited = entry.getEdited();
                     Term match = entry.getTerm(T_APP_MATCH);
                     if (match!=null && match.getValues()!=null) {
                        for (String pattern : match.getValues()) {
                           TemplateRoute route = router.attach(pattern,app);
                           if (exact) {
                              route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                           }
                        }
                     } else {
                        TemplateRoute route = router.attach("",app);
                        if (exact) {
                           route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                        }
                     }
                  } else {
                     applications.put(entry.getId(),new AppInfo(app,entry.getEdited()));
                     Term match = entry.getTerm(T_APP_MATCH);
                     if (match!=null && match.getValues()!=null) {
                        for (String pattern : match.getValues()) {
                           TemplateRoute route = router.attach(pattern,app);
                           if (exact) {
                              route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                           }
                        }
                     } else {
                        TemplateRoute route = router.attach("",app);
                        if (exact) {
                           route.getTemplate().setMatchingMode(Template.MODE_EQUALS);
                        }
                     }
                  }
               }
View Full Code Here

        // Add a route for user's bookmarks resources
        router.attach("/users/{username}/bookmarks", BookmarksResource.class);

        // Add a route for bookmark resources
        final TemplateRoute uriRoute = router.attach(
                "/users/{username}/bookmarks/{URI}", BookmarkResource.class);
        uriRoute.getTemplate().getVariables().put("URI",
                new Variable(Variable.TYPE_URI_ALL));

        return router;
    }
View Full Code Here

        this.router
                .setAttachments(Collections.singletonMap(expected, "timber"));
        registerBeanDefinition("timber", null, TestAuthenticator.class, null);

        doPostProcess();
        TemplateRoute timberRoute = matchRouteFor(expected);
        assertNotNull("No route for " + expected, timberRoute);
        assertTrue("Route is not for correct restlet",
                timberRoute.getNext() instanceof TestAuthenticator);
    }
View Full Code Here

    public void testExplicitAttachmentsTrumpBeanNames() throws Exception {
        this.router.setAttachments(Collections.singletonMap(ORE_URI, "fish"));
        RouteList actualRoutes = actualRoutes();
        assertEquals("Wrong number of routes", 2, actualRoutes.size());

        TemplateRoute oreRoute = matchRouteFor(ORE_URI);
        assertNotNull("No route for " + ORE_URI, oreRoute);
        assertFinderForBean("fish", oreRoute.getNext());
    }
View Full Code Here

    }

    public void testRoutesPointToFindersForBeans() throws Exception {
        final RouteList actualRoutes = actualRoutes();
        assertEquals("Wrong number of routes", 2, actualRoutes.size());
        TemplateRoute oreRoute = matchRouteFor(ORE_URI);
        TemplateRoute fishRoute = matchRouteFor(FISH_URI);
        assertNotNull("ore route not present: " + actualRoutes, oreRoute);
        assertNotNull("fish route not present: " + actualRoutes, fishRoute);

        assertFinderForBean("ore", oreRoute.getNext());
        assertFinderForBean("fish", fishRoute.getNext());
    }
View Full Code Here

TOP

Related Classes of org.restlet.routing.TemplateRoute

Copyright © 2018 www.massapicom. 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.