Package unibg.overencrypt.domain

Source Code of unibg.overencrypt.domain.OverEncryptedFriendsResource

/**
* OverEncrypt project hosted by Università degli Studi di Bergamo
*   -> for PrimeLife project {@link http://www.primelife.eu/}
*/
package unibg.overencrypt.domain;

import java.io.File;
import java.util.Date;

import org.apache.log4j.Logger;

import unibg.overencrypt.core.User;
import unibg.overencrypt.server.ServerConfiguration;

import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.LockInfo;
import com.bradmcevoy.http.LockResult;
import com.bradmcevoy.http.LockTimeout;
import com.bradmcevoy.http.LockToken;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.exceptions.ConflictException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import com.bradmcevoy.http.http11.auth.DigestResponse;

/**
*
* @author Flavio Giovarruscio & Riccardo Tribbia
* @version 1.0
*/
public class OverEncryptedFriendsResource extends OverEncryptedAbstractResource {

  private static final Logger LOGGER = Logger.getLogger(OverEncryptedFriendsResource.class);
 
  protected OverEncryptResourceFactory factory;
  protected File realFile;
  protected boolean allowedClient;
 
  public OverEncryptedFriendsResource(OverEncryptResourceFactory factory, boolean allowedClient){
    this.factory = factory;
    this.allowedClient = allowedClient;
  }
 
  /* (non-Javadoc)
   * @see com.bradmcevoy.http.Resource#authenticate(java.lang.String, java.lang.String)
   */
  public Object authenticate(String user, String password) {
    if(allowedClient) {
      return user;
    }
    else {
      return factory.getSecurityManager().authenticate(user, password);
    }
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.DigestResource#authenticate(com.bradmcevoy.http.http11.auth.DigestResponse)
   */
  public Object authenticate( DigestResponse digestRequest ) {
    if(allowedClient) {
      return digestRequest;
    }
    else {
      return factory.getSecurityManager().authenticate(digestRequest);
    }
  }

  /**
   * Checks if is digest allowed.
   *
   * @return true, if is digest allowed
   */
  public boolean isDigestAllowed() {
    return true;
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.Resource#authorise(com.bradmcevoy.http.Request, com.bradmcevoy.http.Request.Method, com.bradmcevoy.http.Auth)
   */
  public boolean authorise(Request request, Method method, Auth auth) {
    if(allowedClient) return true;
    else return factory.getSecurityManager().authorise(request, method, auth, this);
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.LockableResource#lock(com.bradmcevoy.http.LockTimeout, com.bradmcevoy.http.LockInfo)
   */
  public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
    return factory.getLockManager().lock(timeout, lockInfo, this);
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.LockableResource#refreshLock(java.lang.String)
   */
  public LockResult refreshLock(String token) throws NotAuthorizedException {
    return factory.getLockManager().refresh(token, this);
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.LockableResource#unlock(java.lang.String)
   */
  public void unlock(String tokenId) throws NotAuthorizedException {
    factory.getLockManager().unlock(tokenId, this);
  }

  /* (non-Javadoc)
   * @see com.bradmcevoy.http.LockableResource#getCurrentLock()
   */
  public LockToken getCurrentLock() {
    if( factory.getLockManager() != null ) {
      return factory.getLockManager().getCurrentToken( this );
    } else {
      LOGGER.warn("getCurrentLock called, but no lock manager: file: " + realFile.getAbsolutePath());
      return null;
    }
  }

  @Override
  public void copyTo(CollectionResource arg0, String arg1) {
    //TODO Future development. At the moment, user with permission on this folder
    //that isnt' owner can't copy resource to another collection
  }


  @Override
  public void moveTo(CollectionResource arg0, String arg1)
  throws ConflictException {
    //TODO Future development. At the moment, user with permission on this folder
    //that isnt' owner can't move resource to another collection
  }


  @Override
  public String checkRedirect(Request arg0) {
    return null;
  }
 
  @Override
  public Date getModifiedDate() {
    return null;
  }

  @Override
  public String getName() {
    String name = "";
    try{
      int id  = Integer.parseInt(realFile.getName());
      User user = new User(Integer.toString(id));
      name = user.getUsername();
    } catch (NumberFormatException e) {
      name = realFile.getName();
    }
    return name;
  }

  @Override
  public String getRealm() {
    return ServerConfiguration.getREALM();
  }

  @Override
  public String getUniqueId() {
    String s = realFile.lastModified() + "_" + realFile.length();
    return s.hashCode() + "";
  }
}
TOP

Related Classes of unibg.overencrypt.domain.OverEncryptedFriendsResource

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.