Package org.atomojo.auth.service.app

Source Code of org.atomojo.auth.service.app.UsersResource

/*
* SyncResource.java
*
* Created on April 12, 2007, 1:39 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

package org.atomojo.auth.service.app;

import java.sql.SQLException;
import java.util.UUID;
import java.util.logging.Level;
import org.atomojo.app.client.XMLRepresentationParser;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.User;
import org.atomojo.auth.service.db.XML;
import org.infoset.xml.Document;
import org.infoset.xml.Element;
import org.infoset.xml.util.DocumentDestination;
import org.restlet.Request;
import org.restlet.data.CharacterSet;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.StringRepresentation;
import org.restlet.resource.ServerResource;

/**
*
* @author alex
*/
public class UsersResource extends ServerResource
{
  
   AuthDB db;
   XMLRepresentationParser parser = XML.createParser();
   /** Creates a new instance of SyncResource */
   public UsersResource() {
      setNegotiated(false);
   }

   protected void doInit() {
      db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
      parser.addAllowedElement(XML.USER_NAME);
   }
  
   public Representation get()
   {
      try {
         Representation entity = new DBIteratorRepresentation(MediaType.APPLICATION_XML,XML.USERS_NAME,db.getUsers(),false);
         entity.setCharacterSet(CharacterSet.UTF_8);
         return entity;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get users from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
  
   public Representation post(Representation entity)
   {
      if (!XMLRepresentationParser.isXML(entity.getMediaType())) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Non-XML media type for entity body: "+entity.getMediaType().getName());
      }
      Document doc = null;
     
      try {
         DocumentDestination dest = new DocumentDestination();
         parser.parse(entity,dest);
         doc = dest.getDocument();
         Element top = doc.getDocumentElement();
         String sid = top.getAttributeValue("id");
         UUID id = sid==null ? UUID.randomUUID() : UUID.fromString(sid);
         String alias = top.getAttributeValue("alias");
         String password = top.getAttributeValue("password");
         Element name = top.getFirstElementNamed(XML.NAME_NAME);
         Element email = top.getFirstElementNamed(XML.EMAIL_NAME);
         if (alias!=null && !User.isAlias(alias)) {
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation("The alias '"+alias+"' is not a valid alias.");
         }

         try {
            User user = db.createUser(id,alias,name==null ? null : name.getText(),email==null ? null : email.getText());
            if (user!=null) {
               if (password!=null) {
                  user.setPassword(password);
               }
               Representation responseEntity = new DBObjectRepresentation(MediaType.APPLICATION_XML,user);
               responseEntity.setCharacterSet(CharacterSet.UTF_8);
               getResponse().setStatus(Status.SUCCESS_CREATED);
               return responseEntity;
            } else {
               getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
               return new StringRepresentation("Cannot create user: id="+id+", alias="+alias);
            }
         } catch (SQLException ex) {
            getLogger().log(Level.SEVERE,"Cannot create user with id "+id+", alias "+alias,ex);
            getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
            return new StringRepresentation("Exception during processing, see logs.");
         }
        
        
      } catch (Exception ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("XML parse error: "+ex.getMessage());
      }
   }
  
 
}
TOP

Related Classes of org.atomojo.auth.service.app.UsersResource

TOP
Copyright © 2018 www.massapi.com. 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.