Package org.atomojo.app.auth

Examples of org.atomojo.app.auth.AuthCredentials


  
   public void toXML(ItemDestination dest)
      throws AuthException,XMLException
   {
      AuthService auth = (AuthService)getRequest().getAttributes().get(App.AUTH_SERVICE_ATTR);
      AuthCredentials cred = new AuthCredentials(getRequest().getChallengeResponse());
      final Iterator<User> users = auth.getUsers(cred);

      ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
      dest.send(constructor.createDocument());
      dest.send(constructor.createElement(NM_USERS));
View Full Code Here


      String email = null;
      if (emailE!=null) {
         email = emailE.getText();
      }
      try {
         AuthCredentials cred = new AuthCredentials(getRequest().getChallengeResponse());
         if (auth.getUser(cred,alias)!=null) {
            getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
            return new StringRepresentation("User "+alias+" already exists.");
         } else {
            UUID id = UUID.randomUUID();
View Full Code Here

      AuthService auth = (AuthService)getRequest().getAttributes().get(App.AUTH_SERVICE_ATTR);

      User user = null;
      try {
         ChallengeResponse transCred = getRequest().getChallengeResponse();
         AuthCredentials cred = new AuthCredentials(transCred.getScheme().toString(),transCred.getIdentifier(),new String(transCred.getSecret()));
         user = auth.getUser(cred,alias);
         if (user==null) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("User "+alias+" does not exist.");
         }
View Full Code Here

      }
     
      AuthService auth = (AuthService)getRequest().getAttributes().get(App.AUTH_SERVICE_ATTR);

      ChallengeResponse transCred = getRequest().getChallengeResponse();
      AuthCredentials cred = new AuthCredentials(transCred.getScheme().toString(),transCred.getIdentifier(),new String(transCred.getSecret()));
      if (facet==null) {

         if (!top.getName().equals(NM_USER)) {
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation("Document element "+top.getName()+" not allowed on user.");
View Full Code Here

   }
  
   public Representation delete() {
      AuthService auth = (AuthService)getRequest().getAttributes().get(App.AUTH_SERVICE_ATTR);
      ChallengeResponse transCred = getRequest().getChallengeResponse();
      AuthCredentials cred = new AuthCredentials(transCred.getScheme().toString(),transCred.getIdentifier(),new String(transCred.getSecret()));
      if (facet!=null) {
         if (!facet.equals(GROUPS_FACET)) {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("Unknown facet "+facet);
         } else if (facetAlias==null) {
View Full Code Here

               throw new SyncException("Paths are not current supported for push synchronization.");
            }
            Date lastSync = proc.getLastSynchronizedOn();
            log.info("Synchronizing "+proc.getRemoteApp().getRoot()+", "+(lastSync==null ? "first sync." : "last sync was at "+AtomResource.toXSDDate(lastSync)));
            Iterator<Journal.Entry> entries = db.getJournal().getDeletesSince(lastSync,syncTime);
            AuthCredentials auth = proc.getRemoteApp().getAuthCredentials();
            while (entries.hasNext()) {

               Journal.Entry jentry = entries.next();

               if (jentry.getOperation()==Journal.DELETE_OPERATION) {
                  Journal.DeleteEntry deleteEntry = (Journal.DeleteEntry)jentry;
                  UUID feed = deleteEntry.getFeed();
                  UUID entry = deleteEntry.getEntry();
                  URI feedURI = proc.getRemoteApp().getRoot().resolve(deleteEntry.getPath());
                  if (entry==null) {
                     // delete feed
                     log.info("Deleting feed "+feedURI);
                     FeedClient appClient = new FeedClient(feedURI);
                     if (auth!=null) {
                        appClient.setIdentity(auth.getName(),auth.getPassword());
                     }
                     Status status = appClient.delete();
                     if (!status.isSuccess() && !status.equals(Status.CLIENT_ERROR_NOT_FOUND)) {
                        errorCount++;
                        log.severe("Cannot delete feed "+feedURI+", stopping synchronization.");
                        throw new SyncException("Synchronization was incomplete.  Stopped due to failure to delete feed: "+feedURI);
                     }
                  } else {
                     // delete entry
                     log.info("Deleting entry "+entry+" in feed "+feedURI);
                     FeedClient appClient = new FeedClient(feedURI);
                     if (auth!=null) {
                        appClient.setIdentity(auth.getName(),auth.getPassword());
                     }
                     Status status = appClient.deleteEntry(entry);
                     if (!status.isSuccess() && !status.equals(Status.CLIENT_ERROR_NOT_FOUND)) {
                        errorCount++;
                        log.severe("Cannot delete feed "+feedURI+", stopping synchronization.");
                        throw new SyncException("Synchronization was incomplete.  Stopped due to failure to delete feed: "+feedURI);
                     }
                  }
               }
            }
            entries = db.getJournal().getUpdatesSince(lastSync,syncTime);
            while (entries.hasNext()) {

               Journal.UpdatedEntry updateEntry = (Journal.UpdatedEntry)entries.next();

               Feed feed = updateEntry.getFeed();
               Entry entry = updateEntry.getEntry();
               String path = feed.getPath();
               if (path.length()>0 && !path.endsWith("/")) {
                  path += "/";
               }
               URI feedURI = proc.getRemoteApp().getRoot().resolve(path);
               FeedClient appClient = new FeedClient(feedURI);
               if (auth!=null) {
                  appClient.setIdentity(auth.getName(),auth.getPassword());
               }


               if (updateEntry.getOperation()==Journal.CREATE_OPERATION) {
                  // Create feed or entry
View Full Code Here

     

      log.info("Starting introspection on "+proc.getRemoteApp().getIntrospection());
      final URI root = proc.getRemoteApp().getRoot();
      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()));
                                 }
                              }
                              Date edited = new Date(resource.getEdited().getTime());
                              request.getConditions().setUnmodifiedSince(edited);
                              log.info("Attempting update media from "+srcRef.toString()+", edited="+edited);
View Full Code Here

         name = top.getAttributeValue("name");
         String intro = top.getAttributeValue("introspect");
         introspect = intro==null ? null : URI.create(intro);
         root = URI.create(top.getAttributeValue("root"));
         Element authE = top.getFirstElementNamed(AdminXML.NM_AUTH);
         auth = authE==null ? null : new AuthCredentials(authE.getAttributeValue("method"),authE.getAttributeValue("name"),authE.getAttributeValue("password"));
      } catch (Exception ex) {
         throw new XMLException("Cannot unmarshall XML: "+ex.getMessage(),ex);
      }
   }
View Full Code Here

   {
      URI uri = getIntrospection();
      if (uri==null) {
         uri = getRoot();
      }
      AuthCredentials auth = getAuthCredentials();
      Client client = new Client(new Context(db.getLogger()),Protocol.valueOf(uri.getScheme()));
      client.getContext().getAttributes().put("hostnameVerifier", org.apache.commons.ssl.HostnameVerifier.DEFAULT);
     
      Request request = new Request(Method.HEAD,uri.toString());
      request.setChallengeResponse(new ChallengeResponse(ChallengeScheme.HTTP_BASIC,auth.getName(),auth.getPassword()));
      Response response = client.handle(request);
      return response.getStatus().isSuccess() || response.getStatus().equals(Status.CLIENT_ERROR_NOT_FOUND);
   }
View Full Code Here

TOP

Related Classes of org.atomojo.app.auth.AuthCredentials

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.