Package org.restlet.data

Examples of org.restlet.data.Cookie


   public Representation get()
   {
      Reference service = ActionResource.getReferenceAttribute(getRequest(),"auth-service",confService);
      String name = getCookieName();
      Cookie cookie = null;
      if (name!=null) {
         cookie = getRequest().getCookies().getFirst(name);
         CookieSetting unset = new CookieSetting(name,"");
         unset.setMaxAge(0);
         unset.setPath(getCookiePath());
         getResponse().getCookieSettings().add(unset);
      }
      getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
      if (cookie!=null && service!=null) {
         Client client = new Client(getContext().createChildContext(),service.getSchemeProtocol());
         client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
         Response response = client.handle(new Request(Method.DELETE,service+"/"+cookie.getValue()));
         if (!response.getStatus().isSuccess() && response.getStatus().getCode()!=404) {
            getLogger().warning("Auth service returned "+response.getStatus().getCode()+" for session "+cookie.getValue());
         }
      }
      return null;
   }  
View Full Code Here


      IntrospectionClient client = new IntrospectionClient(log,proc.getRemoteApp().getIntrospection());
      final AuthCredentials auth = proc.getRemoteApp().getAuthCredentials();
      if (auth!=null) {
         if (auth.getScheme().equals("cookie")) {
            log.info("Using cookie based authentication.");
            Cookie cookie = new Cookie(auth.getName(),auth.getPassword());
            cookie.setPath("/");
            client.setCookie(cookie);
         } else {
            log.info("Using identity based authentication.");
            client.setIdentity(auth.getName(),auth.getPassword());
         }
      }
      final Set<String> paths = additive ? null : new TreeSet<String>();
      try {
         client.introspect(new IntrospectionClient.ServiceListener() {
            XMLRepresentationParser parser = new XMLRepresentationParser();
            int workspaceCount = 0;
            public void onStartWorkspace(String title) {
               // TODO: handle more than one workspace
               workspaceCount++;
               log.info("Workspace: "+workspaceCount);
            }
            public void onCollection(EntryCollection collection) {
               if (workspaceCount!=1) {
                  // We only process the first workspace
                  return;
               }
               URI location = collection.getLocation();
               log.info("Processing feed: "+location);
               FeedClient feedClient = new FeedClient(location);
               if (auth!=null) {
                  if (auth.getScheme().equals("cookie")) {
                     Cookie cookie = new Cookie(auth.getName(),auth.getPassword());
                     cookie.setPath("/");
                     feedClient.setCookie(cookie);
                  } else {
                     feedClient.setIdentity(auth.getName(),auth.getPassword());
                  }
               }
               try {
                  Response response = feedClient.get(new FeedDestination() {
                     Set<UUID> entries = additive ? null : new TreeSet<UUID>();
                     Feed feed = null;
                     public void onFeed(Document feedDoc) {
                        URI baseURI = feedDoc.getBaseURI();
                        // Make sure to remove the xml:base on the feed element for storage;
                        feedDoc.getDocumentElement().getAttributes().remove(Attribute.XML_BASE);
                        URI relative = root.relativize(baseURI).normalize();
                        if (relative.isAbsolute()) {
                           log.severe("Cannot make relative URI of '"+baseURI+"' using root '"+root+"'");
                           errorCount++;
                           return;
                        }
                        String fpath = relative.toString();
                        if (fpath.length()>0 && fpath.charAt(fpath.length()-1)!='/') {
                           int lastSlash = fpath.lastIndexOf('/');
                           fpath = fpath.substring(0,lastSlash+1);
                        }
                        log.info("Feed base URI='"+baseURI+"', relative='"+relative+"', feed path='"+fpath+"'");
                        if (!additive) {
                           String [] segments = fpath.split("\\/");
                           String current = null;
                           for (int i=0; i<segments.length; i++) {
                              if (current==null) {
                                 if (segments[i].length()>0) {
                                    current = segments[i]+"/";
                                 } else {
                                    current = segments[i];
                                 }
                              } else {
                                 current += segments[i]+"/";
                              }
                              log.info("Adding path: '"+current+"'");
                              paths.add(current);
                           }
                        }
                        try {
                           feed = app.createFeed(fpath,feedDoc);
                        } catch (AppException ex) {
                           if (ex.getStatus().getCode()==Status.CLIENT_ERROR_CONFLICT.getCode()) {
                              log.info(ex.getMessage());
                              log.info("Feed already exists, retrieving...");
                              try {
                                 feed = app.getFeed(fpath);
                              } catch (AppException cex) {
                                 log.log(Level.SEVERE,"Cannot get feed due to exception.",cex);
                                 errorCount++;
                              }
                              try {
                                 app.updateFeed(feed, feedDoc);
                              } catch (AppException updateEx) {
                                 log.log(Level.SEVERE,"Cannot update feed due to exception.",updateEx);
                                 errorCount++;
                              }
                           } else {
                              log.log(Level.SEVERE,"Failed to create feed due to exception.",ex);
                              errorCount++;
                           }
                        }
                        log.info("Feed ID: "+feed.getUUID()+", path='"+fpath+"'");
                     }
                     public void onEntry(Document entryDoc) {
                        if (feed==null) {
                           return;
                        }
                        entryDoc.getDocumentElement().localizeNamespaceDeclarations();
                        org.atomojo.app.client.Entry index = new org.atomojo.app.client.Entry(entryDoc);
                        index.index();
                        UUID entryId = null;
                        try {
                           String idS = index.getId();
                           if (idS==null) {
                              entryId = UUID.randomUUID();
                              index.setId("urn:uuid:"+entryId);
                              index.update();
                           } else {
                              entryId = UUID.fromString(idS.substring(9));
                           }
                        } catch (IllegalArgumentException ex) {
                           log.severe("Ignoring entry with bad UUID: "+index.getId());
                           errorCount++;
                           return;
                        }
                        log.info("Entry: "+entryId);
                        String src = null;
                        Element content = entryDoc.getDocumentElement().getFirstElementNamed(AtomResource.CONTENT_NAME);
                        URI baseURI = null;
                        MediaType contentType = null;
                        if (content!=null) {
                           src = content.getAttributeValue("src");
                           String type = content.getAttributeValue("type");
                           if (type!=null) {
                              contentType = MediaType.valueOf(type);
                           }
                           baseURI = content.getBaseURI();
                        }
                        if (entries!=null) {
                           entries.add(entryId);
                        }
                        Entry entry = null;
                        try {
                           entry = feed.findEntry(entryId);
                        } catch (SQLException ex) {
                           log.log(Level.SEVERE,"Cannot find entry "+entryId+" due to exception.",ex);
                           errorCount++;
                        }
                        EntryMedia resource = null;
                        boolean hasMedia = false;
                        if (entry!=null) {
                           try {
                              Iterator<EntryMedia> resources = entry.getResources();
                              if (resources.hasNext()) {
                                 hasMedia = true;
                                 while (resources.hasNext()) {
                                    resource = resources.next();
                                    if (!resource.getName().equals(src)) {
                                       resource = null;
                                    }
                                 }
                              }
                           } catch (SQLException ex) {
                              log.log(Level.SEVERE,"Cannot enumerate entry "+index.getId()+" media due to exception.",ex);
                              errorCount++;
                           }
                        }
                        if (entry==null || (src!=null && !hasMedia) || (hasMedia && src==null)) {
                           if (entry!=null) {
                              // delete the entry because it changed to have a media or non-media content (rare)
                              try {
                                 app.deleteEntry(feed,entry);
                              } catch (AppException ex) {
                                 if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                    log.log(Level.SEVERE,ex.getMessage(),ex);
                                 } else {
                                    log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                                 }
                                 errorCount++;
                                 return;
                              }
                           }
                           if (src==null) {
                              // we have a regular entry
                              try {
                                 app.createEntry(user,feed,entryDoc);
                              } catch (AppException ex) {
                                 log.severe("Failed to create entry "+index.getId());
                                 if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                    log.log(Level.SEVERE,ex.getMessage(),ex);
                                 } else {
                                    log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                                 }
                                 errorCount++;
                              }
                           } else {
                              try {
                                 EntryMedia media = feed.findEntryResource(src);
                                 if (media!=null) {
                                    // We have a conflicting media entry.  We'll delete
                                    // the local one to use the pulled one
                                    Entry otherEntry = media.getEntry();
                                    final String fpath = feed.getPath();
                                    otherEntry.delete(new MediaEntryListener() {
                                       public void onDelete(EntryMedia resource) {
                                          try {
                                             storage.deleteMedia(fpath,feed.getUUID(),resource.getName());
                                          } catch (IOException ex) {
                                             log.log(Level.SEVERE,"Cannot delete media "+resource.getName(),ex);
                                          }
                                       }
                                    });
                                    storage.deleteEntry(fpath,feed.getUUID(),otherEntry.getUUID());
                                 }
                              } catch (SQLException ex) {
                                 log.log(Level.SEVERE,"Database error while processing local media reference "+src,ex);
                              } catch (IOException ex) {
                                 log.log(Level.SEVERE,"I/O error while deleting entry for media "+src,ex);
                              }
                              // we have a media entry
                              URI srcRef = baseURI.resolve(src);

                              Client client = new Client(new Context(log),Protocol.valueOf(srcRef.getScheme()));
                              client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
                              Request request = new Request(Method.GET,new Reference(srcRef.toString()));
                              if (auth!=null) {
                                 if (auth.getScheme().equals("cookie")) {
                                    Cookie cookie = new Cookie(auth.getName(),auth.getPassword());
                                    cookie.setPath("/");
                                    request.getCookies().add(cookie);
                                 } else {
                                    request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,auth.getName(),auth.getPassword()));
                                 }
                              }
                              Response response = client.handle(request);
                              if (!response.getStatus().isSuccess()) {
                                 log.log(Level.SEVERE,"Failed to retrieve media, status="+response.getStatus().getCode()+", src="+srcRef);
                                 errorCount++;
                                 return;
                              }
                              if (contentType!=null) {
                                 // The entry's media type wins.  Sometimes file resources do not
                                 // report the media type correctly
                                 response.getEntity().setMediaType(contentType);
                              }
                              try {
                                 entry = app.createMediaEntry(user,feed,response.getEntity(),src,entryId);
                                 app.updateEntry(user,feed,entry,entryDoc);
                              } catch (AppException ex) {
                                 log.severe("Failed to create media entry "+index.getId()+", src="+srcRef);
                                 if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                    log.log(Level.SEVERE,ex.getMessage(),ex);
                                 } else {
                                    log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                                 }
                                 errorCount++;
                              }
                           }
                        } else {
                           try {
                              app.updateEntry(user,feed,entryId,entryDoc);
                           } catch (AppException ex) {
                              if (ex.getStatus()==Status.SERVER_ERROR_INTERNAL) {
                                 log.log(Level.SEVERE,ex.getMessage(),ex);
                              } else {
                                 log.severe("Status="+ex.getStatus().getCode()+", "+ex.getMessage());
                              }
                              errorCount++;
                              return;
                           }
                           if (src!=null) {
                              URI srcRef = baseURI.resolve(src);

                              Client client = new Client(new Context(log),Protocol.valueOf(srcRef.getScheme()));
                              client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
                              /*
                             
                              Request headRequest = new Request(Method.HEAD,new Reference(srcRef.toString()));
                              if (auth!=null) {
                                 headRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,auth.getName(),auth.getPassword()));
                              }
                              Response headResponse = client.handle(headRequest);
                              if (!headResponse.getStatus().isSuccess()) {
                                 log.log(Level.SEVERE,"Failed to retrieve media head, status="+headResponse.getStatus().getCode()+", src="+srcRef);
                                 errorCount++;
                                 return;
                              }
                              boolean outOfDate = true;
                              if (headResponse.isEntityAvailable()) {
                                 outOfDate = headResponse.getEntity().getModificationDate().after(resource.getEdited());
                                 if (outOfDate) {
                                    log.info("Out of date: "+headResponse.getEntity().getModificationDate()+" > "+resource.getEdited());
                                 }
                              }
                               */
                             
                              Request request = new Request(Method.GET,new Reference(srcRef.toString()));
                              if (auth!=null) {
                                 if (auth.getScheme().equals("cookie")) {
                                    Cookie cookie = new Cookie(auth.getName(),auth.getPassword());
                                    cookie.setPath("/");
                                    request.getCookies().add(cookie);
                                 } else {
                                    request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,auth.getName(),auth.getPassword()));
                                 }
                              }
View Full Code Here

            } else if (getRequest().getChallengeResponse()!=null) {
                // pass along authentication
               getLogger().info("Passing along authentication to APP target.");
               appRequest.setChallengeResponse(getRequest().getChallengeResponse());
            }
            Cookie cookie = getRequest().getCookies().getFirst("I");
            if (cookie!=null) {
               appRequest.getCookies().add(cookie);
            }
           
            // add header for slug if new upload
View Full Code Here

      final String base = getContext().getParameters().getFirstValue("app.url");
      getLogger().info("app.url="+base);
      Filter requireAuth = new Filter(getContext()) {
         protected int beforeHandle(Request request,Response response)
         {
            Cookie cookie = request.getCookies().getFirst("I");
            if (request.getChallengeResponse()==null || cookie==null) {
               String baseURL = base;
               if (baseURL==null) {
                  Object o = request.getAttributes().get("app.url");
                  if (o!=null) {
View Full Code Here

                     client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
                     Request remoteRequest = new Request(Method.GET,new Reference(resource.toString()));
                     if (base.getUsername()!=null) {
                        remoteRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,base.getUsername(),base.getPassword()));
                     }
                     Cookie cookie = request.getCookies().getFirst("I");
                     if (cookie!=null) {
                        remoteRequest.getCookies().add(cookie);
                     }
                     remoteResponse = client.handle(remoteRequest);
                  }
View Full Code Here

                     }
                  }
               }
            }
            if (allowSessions) {
               Cookie cookie = request.getCookies().getFirst("I");
               if (cookie!=null) {
                  try {
                     Identity identity = checkSession(getContext().createChildContext(),RealmGuard.this.service,cookie.getValue());
                     if (identity!=null) {
                        request.getAttributes().put(Identity.IDENTITY_ATTR,identity);
                        return Verifier.RESULT_VALID;
                     }
                  } catch (RequestFailedException ex) {
View Full Code Here

            client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
            remoteRequest = new Request(Method.GET,new Reference(resource.toString()));
            if (username!=null) {
               remoteRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,username,password));
            }
            Cookie cookie = getRequest().getCookies().getFirst("I");
            if (cookie!=null) {
               remoteRequest.getCookies().add(cookie);
            }
         }
         final Response resourceResponse = remoteRequest==null ? null : client.handle(remoteRequest);
View Full Code Here

         appRequest.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,username,password));
      } else if (request.getChallengeResponse()!=null) {
         appRequest.setChallengeResponse(request.getChallengeResponse());
      }
      appRequest.setEntity(request.getEntity());
      Cookie cookie = request.getCookies().getFirst("I");
      if (cookie!=null) {
         appRequest.getCookies().add(cookie);
      }
      Form headers = (Form)request.getAttributes().get("org.restlet.http.headers");
      if (headers!=null) {
View Full Code Here

   }
  
   protected int beforeHandle(Request request,Response response)
   {
      Reference service = getReferenceAttribute(request,"auth-service",confService);
      Cookie cookie = request.getCookies().getFirst(cookieName);
      if (cookie!=null) {
         getLogger().info("cookie: "+cookieName+"="+cookie.getValue());
         Identity identity = identities.get(cookie.getValue());
         if (identity!=null) {
            getLogger().info("Found "+identity.getAlias()+", checking age...");
            // check age
            Long created = createdTimes.get(cookie.getValue());
            if (created==null) {
               getLogger().info("Expiring.");
               identities.remove(cookie.getValue());
               identity = null;
            } else if ((System.currentTimeMillis()-created.longValue())>=maxAge) {
               removeIdentity(cookie.getValue());
               identity = null;
            }
         }
         if (identity==null) {
            // TODO: make this configurable
            if (!lookup) {
               return Filter.CONTINUE;
            }
            getLogger().info("Looking up identity "+cookie.getValue());
            try {
               identity = RealmGuard.checkSession(getContext().createChildContext(),service, cookie.getValue());
            } catch (RequestFailedException ex) {
               if (ex.getStatus()!=null) {
                  getLogger().severe("Cannot check session against service, status="+ex.getStatus().getCode());
               } else {
                  getLogger().log(Level.SEVERE,"Cannot authenticate against service.",ex);
               }
            }
            if (identity!=null) {
               addIdentity(cookie.getValue(),identity);
            }
         }
         if (identity!=null) {
            addIdentity(request,identity);
         }
View Full Code Here

  private Request getRequestWithCookie(Method method, String resourceUri)
  {
    Request request = new Request(method, resourceUri);
    request.setMethod(method);
    Series<Cookie> cookies = request.getCookies();
    cookies.add(new Cookie("infinitecookie","api:"+test_api_key));
    request.setCookies(cookies);
    return request;
  }
View Full Code Here

TOP

Related Classes of org.restlet.data.Cookie

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.