Esempio n. 1
0
  @Override
  public Resource getResource(String host, String sPath)
      throws NotAuthorizedException, BadRequestException {
    LogUtils.trace(log, "getResource", host, sPath);
    Path path = Path.path(sPath);
    Path parent = path.getParent();
    Request request = HttpManager.request();
    String encodedPath = request.getAbsolutePath();

    // This is to support a use case where a developer wants their resources to
    // be accessible through milton-json, but don't want to use DAV urls. Instead
    // they use a parameter and DO NOT implement PostableResource.
    if (request.getMethod().equals(Method.POST)) {
      Resource wrappedResource = wrapped.getResource(host, sPath);
      if (wrappedResource != null && !(wrappedResource instanceof PostableResource)) {
        LogUtils.trace(log, "getResource: is post, and got a: ", wrappedResource.getClass());
        return new PostJsonResource(host, encodedPath, wrappedResource, methodParamName, this);
      }
    }
    if (request.getMethod().equals(Method.GET)
        && isMatchingContentType(request.getAcceptHeader())) {
      Resource wrappedResource = wrapped.getResource(host, sPath);
      if (wrappedResource != null) {
        log.trace("getResource: matches content type, and found wrapped resource");
        return wrapResource(host, wrappedResource, Method.PROPFIND.code, encodedPath);
      } else {
        LogUtils.trace(
            log, "getResource: is GET and matched type, but found no actual resource on", sPath);
      }
    }
    if (isMatchingPath(parent)) {
      log.trace("getResource: is matching path");
      Path resourcePath = parent.getParent();
      if (resourcePath != null) {
        String method = path.getName();
        Resource wrappedResource = wrapped.getResource(host, resourcePath.toString());
        if (wrappedResource != null) {
          Resource r = wrapResource(host, wrappedResource, method, encodedPath);
          LogUtils.trace(log, "returning a", r.getClass());
          return r;
        }
      }
    } else {
      log.trace("getResource: not matching path");
      return wrapped.getResource(host, sPath);
    }
    return null;
  }
Esempio n. 2
0
  public synchronized void doGet(Path path, final java.io.File file, ProgressListener listener)
      throws IOException, NotFoundException, com.ettrema.httpclient.HttpException,
          CancelledException, NotAuthorizedException, BadRequestException, ConflictException {
    LogUtils.trace(log, "doGet", path);
    if (fileSyncer != null) {
      fileSyncer.download(this, path, file, listener);
    } else {
      String url = this.buildEncodedUrl(path);
      transferService.get(
          url,
          new StreamReceiver() {

            @Override
            public void receive(InputStream in) throws IOException {
              OutputStream out = null;
              BufferedOutputStream bout = null;
              try {
                out = FileUtils.openOutputStream(file);
                bout = new BufferedOutputStream(out);
                IOUtils.copy(in, bout);
                bout.flush();
              } finally {
                IOUtils.closeQuietly(bout);
                IOUtils.closeQuietly(out);
              }
            }
          },
          null,
          listener);
    }
  }
Esempio n. 3
0
 /**
  * Uploads the data. Does not do any file syncronisation
  *
  * @param newUri - encoded full URL
  * @param content
  * @param contentLength
  * @param contentType
  * @return - the result code
  */
 public synchronized int doPut(
     String newUri,
     InputStream content,
     Long contentLength,
     String contentType,
     ProgressListener listener) {
   LogUtils.trace(log, "doPut", newUri);
   return transferService.put(newUri, content, contentLength, contentType, listener);
 }
Esempio n. 4
0
 public Resource wrapResource(String host, Resource wrappedResource, String method, String href) {
   LogUtils.trace(log, "wrapResource: ", method);
   if (Request.Method.PROPFIND.code.equals(method)) {
     if (wrappedResource instanceof PropFindableResource) {
       return new PropFindJsonResource(
           (PropFindableResource) wrappedResource, propFindHandler, href, maxAgeSecsPropFind);
     } else {
       log.warn(
           "Located a resource for PROPFIND path, but it does not implement PropFindableResource: "
               + wrappedResource.getClass());
     }
   }
   if (Request.Method.PROPPATCH.code.equals(method)) {
     return new PropPatchJsonResource(wrappedResource, propPatchHandler, href);
   }
   if (Request.Method.PUT.code.equals(method)) {
     if (wrappedResource instanceof PutableResource) {
       return new PutJsonResource((PutableResource) wrappedResource, href);
     }
   }
   if (Request.Method.MKCOL.code.equals(method)) {
     if (wrappedResource instanceof MakeCollectionableResource) {
       return new MkcolJsonResource(
           (MakeCollectionableResource) wrappedResource, href, eventManager);
     }
   }
   if (Request.Method.COPY.code.equals(method)) {
     if (wrappedResource instanceof CopyableResource) {
       return new CopyJsonResource(host, (CopyableResource) wrappedResource, wrapped);
     }
   }
   if (Request.Method.MOVE.code.equals(method)) {
     if (wrappedResource instanceof MoveableResource) {
       return new MoveJsonResource(host, (MoveableResource) wrappedResource, wrapped);
     }
   }
   return wrappedResource;
 }
Esempio n. 5
0
 /**
  * @param newUri
  * @param file
  * @param listener
  * @return - the result code
  * @throws FileNotFoundException
  * @throws HttpException
  */
 public int doPut(Path remotePath, java.io.File file, ProgressListener listener)
     throws FileNotFoundException, HttpException, CancelledException, NotAuthorizedException,
         ConflictException {
   if (fileSyncer != null) {
     try {
       fileSyncer.upload(this, file, remotePath, listener);
       LogUtils.trace(log, "doPut: uploaded");
       return Response.Status.SC_OK.code;
     } catch (NotFoundException e) {
       // ZSync file was not found
       log.trace("Not found: " + remotePath);
     } catch (IOException ex) {
       throw new GenericHttpException(remotePath.toString(), ex);
     }
   }
   InputStream in = null;
   try {
     in = new FileInputStream(file);
     String dest = buildEncodedUrl(remotePath);
     return doPut(dest, in, file.length(), null, listener);
   } finally {
     IOUtils.closeQuietly(in);
   }
 }