Package unibg.overencrypt.protocol

Source Code of unibg.overencrypt.protocol.OverEncryptRequest

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

import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.InetAddress;

import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.provider.webdav.WebdavFileObject;
import org.apache.log4j.Logger;

import unibg.overencrypt.client.OverEncryptClient;
import unibg.overencrypt.client.managers.WebDAVClientManager;

/**
* Manages the request at client-side.
*
* @author Flavio Giovarruscio & Riccardo Tribbia
* @version 1.0
*/
public class OverEncryptRequest {

  /** Logger for this class. */
  private static final Logger LOGGER = Logger.getLogger(OverEncryptRequest.class);

  public enum OverEncryptRequestType{
    LOCK, UNLOCK, CONTINUE;
  }
 
  /// Last http client instance retrieved from communication channel*/
  //private static HttpClient httpClient = null;
 
  /**
   * Generates a request.
   *
   * @param primitive the client primitive in the request
   * @param type of operation (LOCK, UNLOCK or CONTINUE)
   * @param userId the userId that execute the operation
   * @param path the actual path to communication with server throught .response / .request
   * @param values the values to insert in request
   * @return true, if successful
   */
  public static boolean generateRequest(ClientPrimitives primitive, OverEncryptRequestType opType, String userId, String path, String... values) {
    boolean allOk = false;
   
    OutputStream fileWriter = null;
    FileSystemManager vfsManager = null;
   
    //Get a Virtual File System Manager
    vfsManager = WebDAVClientManager.getVFSManager();
    if(vfsManager == null)LOGGER.debug("vfsManager is null");
   
    switch (opType) {
    case LOCK:
      try{
        //send lock request thought ghost file name
        LOGGER.debug("webdav path: " + path.replace("dav", "webdav") + "/.lock_" + userId); //TODO Retrieve client unique identifier
        WebdavFileObject fileObject = (WebdavFileObject) vfsManager.resolveFile(path.replace("dav", "webdav") + "/.lock_" + userId);
        fileWriter = fileObject.getOutputStream();
      }catch(Exception e){
        LOGGER.debug("It's all ok.. sending lock request");
      }
      allOk = true;
      break;
    case UNLOCK:
      try{
        //send unlock request thought ghost file name
        LOGGER.debug("webdav path: " + path.replace("dav", "webdav") + "/.unlock_" + userId); //TODO Retrieve client unique identifier
        WebdavFileObject fileObject = (WebdavFileObject) vfsManager.resolveFile(path.replace("dav", "webdav") + "/.unlock_" + userId);
        fileWriter = fileObject.getOutputStream();
      }catch(Exception e){
        LOGGER.debug("It's all ok.. sending unlock request");
      }
      allOk = true;
      break;
    case CONTINUE:
      allOk = sendRequest(vfsManager, fileWriter, path, primitive, values);
      break;
    }
   
    return allOk;
  }
 
  private static boolean sendRequest(FileSystemManager vfsManager, OutputStream fileWriter, String path, ClientPrimitives primitive, String... values ){
    boolean allOk = false;
    try{
      WebdavFileObject fileObject = (WebdavFileObject) vfsManager.resolveFile(path.replace("dav", "webdav") + "/.request");
     
      //TODO Se la Risorsa virtuale rimane montata sul file system del client questa funzione
      //genera dei problemi in quanto ottiene l'output stream dalla connessione che al momento non c'è
      fileWriter = fileObject.getOutputStream();
      //Use it
      PrintStream write = new PrintStream(fileWriter);
      write.println(primitive.toString() + " \"" + path + "\" HTTP/1.1");
      write.println("host: " + InetAddress.getLocalHost());
      for (int i = 0; i < primitive.getArgs().length; i++) {
        write.println(primitive.getArgs()[i] + "=\"" + values[i] + "\"");     
      }
      write.println("user-agent: " + OverEncryptClient.getName());
      allOk = true;

    }catch (Exception e) {
      LOGGER.error("Error while writing client request - ",e);
     
      //TODO Provare una riconnessione con le ultime credenziali utilizzate?
      //WebDavFileSystem webdavFileSystem = (WebDavFileSystem) fileObject.getFileSystem();
      //LOGGER.debug("0");
      //httpClient =  webdavFileSystem.getClient();
      //LOGGER.debug("1 - " + httpClient.getHost() + " - " + httpClient.getPort());
      //LOGGER.debug("authScope: " + new AuthScope(httpClient.getHost(), httpClient.getPort()).toString());
      //lastCredentialsMet = (UsernamePasswordCredentials) httpClient.getState().getCredentials(new AuthScope(httpClient.getHost(), httpClient.getPort()));
      //LOGGER.debug("lastCredentialsMet user: " + lastCredentialsMet.getUserName() + ", password: " + lastCredentialsMet.getPassword());
 
      //HttpConnection webDavConnection = new HttpConnection("localhost", 8080, Protocol.getProtocol("dav"));
      //Credentials lastUsedCredential = new UsernamePasswordCredentials("ricky.tribbia", "ricky");
      //HttpClient
      //AuthScope authScope = new AuthScope(httpClient.getHost(), httpClient.getPort(), "OverEncryption Demo Server Realm", "BASIC");
      //httpClient.getState().setCredentials(authScope, new UsernamePasswordCredentials("ricky.tribbia","ricky"));
      //httpClient.getState().setCredentials(new AuthScope(httpClient.getHost(), httpClient.getPort()), new UsernamePasswordCredentials("ricky.tribbia","ricky"));         
   
    } finally {
      if(fileWriter != null){
        try {
          fileWriter.close();
        } catch (IOException e) {
          LOGGER.warn("Error while closing file output stream",e);
        }
      }
    }
    return allOk;
  }
}
TOP

Related Classes of unibg.overencrypt.protocol.OverEncryptRequest

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.