Package org.atomojo.auth.service.app

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

/*
* 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.Group;
import org.atomojo.auth.service.db.Realm;
import org.atomojo.auth.service.db.Role;
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 RealmsResource extends ServerResource
{
  
   AuthDB db;
   XMLRepresentationParser parser = XML.createParser();
   /** Creates a new instance of SyncResource */
   public RealmsResource() {
      setNegotiated(false);
   }

   protected void doInit() {
      db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
      parser.addAllowedElement(XML.REALM_NAME);
   }
  
   public Representation get()
   {
      try {
         Representation entity = new DBIteratorRepresentation(MediaType.APPLICATION_XML,XML.REALMS_NAME,db.getRealms(),false);
         entity.setCharacterSet(CharacterSet.UTF_8);
         return entity;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get realms 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 name = top.getAttributeValue("name");

         try {
            Role rootRole = db.getRole(AuthDB.REALM_ROOT_ROLE);
            if (rootRole==null) {
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
               return new StringRepresentation("Realm root role not found.");
            }
            Role editorRole = db.getRole(AuthDB.EDITOR_ROLE);
            if (editorRole==null) {
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
               return new StringRepresentation("Editor role not found.");
            }
            Realm realm = db.getRealm(name);
            if (realm!=null) {
               getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
               return new StringRepresentation("Realm "+name+" already exists.");
            }
            realm = db.getRealm(id);
            if (realm!=null) {
               getResponse().setStatus(Status.CLIENT_ERROR_CONFLICT);
               return new StringRepresentation("Realm with id "+id+" already exists.");
            }
           
            realm = db.createRealm(name,id);
            try {
               Group group = db.createGroup(realm,UUID.randomUUID(),"admin");
               group.addRole(rootRole);
               group.addRole(editorRole);
               Representation responseEntity = new DBObjectRepresentation(MediaType.APPLICATION_XML,realm);
               responseEntity.setCharacterSet(CharacterSet.UTF_8);
               getResponse().setStatus(Status.SUCCESS_CREATED);
               return responseEntity;
            } catch (SQLException ex) {
               getContext().getLogger().log(Level.SEVERE,"Cannot create realm admin group.",ex);
               try {
                  realm.delete();
               } catch (SQLException ex2) {
                  getContext().getLogger().log(Level.SEVERE,"Cannot delete realm ("+name+","+id+") for cleanup",ex2);
               }
               getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
               return new StringRepresentation("Cannot create realm admin group.");
            }
         } catch (SQLException ex) {
            getContext().getLogger().log(Level.SEVERE,"Cannot create realm.",ex);
            getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
            return new StringRepresentation("Realm creation refused.");
         }
        
        
      } 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.RealmsResource

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.