Exemplo n.º 1
0
 @Override
 public void handle(Request request, Response response) {
   final StringBuilder sb = new StringBuilder();
   final RouteList routers = router.getRoutes();
   final String prefix =
       request.getOriginalRef().getPath().endsWith("/")
           ? ""
           : request.getOriginalRef().getPath() + "/";
   final boolean detailed =
       request.getOriginalRef().getQueryAsForm().getNames().contains("describe");
   sb.append("<div>");
   if (detailed) {
     String href = request.getOriginalRef().getPath();
     sb.append("<a href='" + href + "'>[Less Detail]</a>");
   } else {
     sb.append("<a href='?describe'>[More Detail]</a>");
   }
   sb.append("<ul>");
   for (int i = 0; i < routers.size(); i++) {
     Route r = routers.get(i);
     if (!detailed && r.getTemplate().getVariableNames().size() > 0) {
       continue;
     }
     String pattern = r.getTemplate().getPattern();
     if (pattern.length() > 0) {
       pattern = pattern.substring(1);
       if (pattern.length() > 0) {
         sb.append("<li>");
         sb.append("<a href='" + prefix + pattern + "'>" + pattern + "</a>");
         if (detailed) {
           Restlet next = r.getNext();
           Class dest = null;
           if (next instanceof Finder) {
             dest = ((Finder) next).getTargetClass();
           } else if (next instanceof Guard) {
             Guard g = (Guard) next;
             if (g.getNext() instanceof Finder) {
               dest = ((Finder) g.getNext()).getTargetClass();
             } else {
               dest = g.getNext().getClass();
             }
           }
           if (dest != null) {
             sb.append("<span> [").append(dest.getName()).append(" ]</span>");
           }
           try {
             String description = findDescription(dest);
             if (description != null) {
               sb.append("<div>").append(description).append("</div>");
             }
           } catch (Exception ex) {
             getLogger().log(Level.SEVERE, "Error getting description", ex);
           }
         }
         sb.append("</li>");
       }
     }
   }
   sb.append("</ul>");
   sb.append(
       "PSCP API, Version " + Version.getVersion() + ", Created " + Version.getVersionDate());
   sb.append("</div>");
   response.setEntity(sb.toString(), MediaType.TEXT_HTML);
 }
Exemplo n.º 2
0
 private Guard guard(Restlet resource) {
   Guard guard = createGuard();
   guard.setNext(resource);
   return guard;
 }
Exemplo n.º 3
0
 private Guard guard(Class<? extends Resource> resource) {
   Guard guard = createGuard();
   guard.setNext(resource);
   return guard;
 }
Exemplo n.º 4
0
  private Guard createGuard() {
    Guard guard =
        new Guard(getContext(), "PSCP", baseURIs, "jjaahh") {

          @Override
          public boolean checkSecret(Request request, String identifier, char[] secret) {
            if ("logout".equals(identifier)) {
              getLogger().log(Level.INFO, "Invalid login attempt for account '" + identifier + "'");
              return false;
            }
            return super.checkSecret(request, identifier, secret);
          }

          @Override
          public void accept(Request request, Response response) {
            super.accept(request, response);
            String user = request.getChallengeResponse().getParameters().getFirstValue("username");
            if (user != null) {
              String cookieVal = request.getCookies().getFirstValue("u");
              if (cookieVal == null || cookieVal.length() == 0) {
                UUID id = findContactID(user);
                if (id != null) {
                  setCookie(id, response);
                  request.getAttributes().put(Contacts.class.getName(), id.toString());
                  trackLogin(id);
                  lookupCache.daoModified(Contacts.class);
                } else {
                  getLogger().warning("Cannot find db entry for user : "******"workaround" plugin for Postgres
                // Workaround is to use string instead of value
                id = UUID.fromString(r.string(Contacts.CONTACTID));
                break;
              }
            }
            return id;
          }

          @Override
          public void forbid(Response response) {
            super.forbid(response);
            setCookie(null, response);
          }

          @Override
          public void challenge(Response response, boolean stale) {
            super.challenge(response, stale);
          }

          private void setCookie(UUID user, Response response) {
            CookieSetting cookie = new CookieSetting("u", user == null ? "" : user.toString());
            cookie.setPath("/");
            response.getCookieSettings().add(cookie);
          }

          private void trackLogin(UUID user) {
            DAOCollection daos = null;
            try {
              daos = daoFactory.create();
              daos.get(Contacts.class).login(user);
            } catch (DAOException ex) {
              getLogger().log(Level.SEVERE, "Error tracking login", ex);
            } finally {
              if (daos != null) {
                daos.close();
              }
            }
          }
        };
    guard.setSecretResolver(secretResolver);
    return guard;
  }