Package org.atomojo.auth.service.app

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

/*
* 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.logging.Level;
import org.atomojo.auth.service.db.AuthDB;
import org.atomojo.auth.service.db.Realm;
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 RealmResource extends ServerResource
{
  
   AuthDB db;
   /** Creates a new instance of SyncResource */
   public RealmResource() {
      setNegotiated(false);
   }

   protected void doInit() {
      db = (AuthDB)getRequest().getAttributes().get(AuthApplication.DB_ATTR);
   }
  
   public Representation get()
   {
      try {
         Realm realm = fetch();
         if (realm!=null) {
            Representation entity = new DBObjectRepresentation(MediaType.APPLICATION_XML,realm);
            entity.setCharacterSet(CharacterSet.UTF_8);
            return entity;
         }
         getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         return null;
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Cannot get realm from database.",ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
  
   protected Realm fetch()
      throws SQLException
   {
      /*
      Realm realm = null;
      if (name!=null) {
         realm = db.getRealm(name);
      }
      if (suuid!=null) {
         UUID id = UUID.fromString(suuid);
         realm = db.getRealm(id);
      }
      return realm;
       */
      return (Realm)getRequest().getAttributes().get(AuthApplication.REALM_ATTR);
   }
  
   public Representation delete() {
      try {
         Realm realm = fetch();
         if (realm!=null) {
            realm.delete();
            getResponse().setStatus(Status.SUCCESS_NO_CONTENT);
         } else {
            getResponse().setStatus(Status.CLIENT_ERROR_NOT_FOUND);
         }
         return null;
      } catch (IllegalArgumentException ex) {
         getResponse().setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
         return new StringRepresentation("Bad UUID value specified: "+ex.getMessage());
      } catch (SQLException ex) {
         getContext().getLogger().log(Level.SEVERE,"Database error during realm delete: "+ex.getMessage(),ex);
         getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
         return new StringRepresentation("Exception during processing, see logs.");
      }
   }
 
}
TOP

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

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.