/**
* OverEncrypt project hosted by Università degli Studi di Bergamo
* -> for PrimeLife project {@link http://www.primelife.eu/}
*/
package unibg.overencrypt.domain;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import org.apache.log4j.Logger;
import unibg.overencrypt.core.User;
import unibg.overencrypt.server.ResourcesManager;
import unibg.overencrypt.server.ServerConfiguration;
import com.bradmcevoy.http.Auth;
import com.bradmcevoy.http.CollectionResource;
import com.bradmcevoy.http.DeletableResource;
import com.bradmcevoy.http.GetableResource;
import com.bradmcevoy.http.LockInfo;
import com.bradmcevoy.http.LockResult;
import com.bradmcevoy.http.LockTimeout;
import com.bradmcevoy.http.LockToken;
import com.bradmcevoy.http.LockingCollectionResource;
import com.bradmcevoy.http.MakeCollectionableResource;
import com.bradmcevoy.http.PropFindableResource;
import com.bradmcevoy.http.PutableResource;
import com.bradmcevoy.http.Range;
import com.bradmcevoy.http.Request;
import com.bradmcevoy.http.Request.Method;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.exceptions.BadRequestException;
import com.bradmcevoy.http.exceptions.ConflictException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import com.bradmcevoy.http.exceptions.PreConditionFailedException;
import com.bradmcevoy.http.http11.auth.DigestResponse;
/**
* Manages the OverEncrypted shared folder.
*
* @author Flavio Giovarruscio & Riccardo Tribbia
* @version 1.0
*/
public class OverEncryptedSharedFolder extends OverEncryptedAbstractResource
implements MakeCollectionableResource,
PutableResource, DeletableResource, PropFindableResource,
LockingCollectionResource, GetableResource{
/** Logger for this class. */
private static final Logger LOGGER = Logger.getLogger( OverEncryptedSharedFolder.class );
/** The OverEncrypt factory. */
protected final OverEncryptResourceFactory factory;
protected final int loggedUserID;
public OverEncryptedSharedFolder(OverEncryptResourceFactory factory, int loggedUser){
LOGGER.debug("Shared folder built");
this.factory = factory;
this.loggedUserID = loggedUser;
}
@Override
public Resource child(String username) {
return new OverEncryptedFriendsFolder(factory, username, User.getId(username),loggedUserID);
}
@Override
public List<? extends Resource> getChildren() {
LOGGER.debug("Shared folder get children");
ArrayList<Resource> list = new ArrayList<Resource>();
ResourcesManager resMan = (ResourcesManager)factory;
HashMap<Integer,ArrayList<String>> permissions = resMan.getUserPermissions(loggedUserID);
Set<Integer> keys = permissions.keySet();
for (Iterator<Integer> iterator = keys.iterator(); iterator.hasNext();) {
Integer userThatSharedResources = (Integer) iterator.next();
if(userThatSharedResources != loggedUserID)
list.add(new OverEncryptedFriendsFolder(factory, Integer.toString(userThatSharedResources), userThatSharedResources, loggedUserID));
}
return list;
}
@Override
public Long getContentLength() {
return null;
}
@Override
public String getContentType(String arg0) {
return "text/html";
}
@Override
public Long getMaxAgeSeconds(Auth arg0) {
return null;
}
@Override
public void sendContent(OutputStream arg0, Range arg1,
Map<String, String> arg2, String arg3) throws IOException,
NotAuthorizedException, BadRequestException {
//Do nothing
}
@Override
public LockToken createAndLock(String arg0, LockTimeout arg1, LockInfo arg2)
throws NotAuthorizedException {
return null;
}
@Override
public Date getCreateDate() {
GregorianCalendar cal = new GregorianCalendar();
return cal.getTime();
}
@Override
public void delete() throws NotAuthorizedException, ConflictException,
BadRequestException {
//Do nothing
}
@Override
public Resource createNew(String arg0, InputStream arg1, Long arg2,
String arg3) throws IOException, ConflictException {
return null;
}
@Override
public CollectionResource createCollection(String name)
throws NotAuthorizedException, ConflictException {
return null;
}
@Override
public Object authenticate(DigestResponse digestResponse) {
return digestResponse;
}
@Override
public LockToken getCurrentLock() {
if( factory.getLockManager() != null ) {
return factory.getLockManager().getCurrentToken( this );
} else {
return null;
}
}
@Override
public LockResult lock(LockTimeout timeout, LockInfo lockInfo) throws NotAuthorizedException {
return factory.getLockManager().lock(timeout, lockInfo, this);
}
@Override
public LockResult refreshLock(String token) throws NotAuthorizedException,
PreConditionFailedException {
return factory.getLockManager().refresh(token, this);
}
@Override
public void unlock(String tokenId) throws NotAuthorizedException,
PreConditionFailedException {
factory.getLockManager().unlock(tokenId, this);
}
@Override
public void copyTo(CollectionResource arg0, String arg1) {
//Do nothing
}
@Override
public void moveTo(CollectionResource arg0, String arg1)
throws ConflictException {
//Do nothing
}
@Override
public Object authenticate(String user, String password) {
return user;
}
@Override
public boolean authorise(Request request, Method method, Auth auth) {
return true;
}
@Override
public String checkRedirect(Request arg0) {
return null;
}
@Override
public Date getModifiedDate() {
GregorianCalendar cal = new GregorianCalendar();
return cal.getTime();
}
@Override
public String getName() {
return "Shared";
}
@Override
public String getRealm() {
return ServerConfiguration.getREALM();
}
@Override
public String getUniqueId() {
return Long.toString((new Random().nextLong()));
}
public boolean isDigestAllowed() {
// TODO Auto-generated method stub
return false;
}
}