@Override
 public void respondHead(Resource resource, Response response, Request request) {
   // setRespondContentCommonHeaders(response, resource, Response.Status.SC_NO_CONTENT,
   // request.getAuthorization());
   setRespondContentCommonHeaders(
       response, resource, Response.Status.SC_OK, request.getAuthorization());
   if (!(resource instanceof GetableResource)) {
     return;
   }
   GetableResource gr = (GetableResource) resource;
   Long contentLength = gr.getContentLength();
   if (contentLength != null) {
     response.setContentLengthHeader(contentLength);
   } else {
     log.trace("No content length is available for HEAD request");
   }
   String acc = request.getAcceptHeader();
   String ct = gr.getContentType(acc);
   if (ct != null) {
     ct = pickBestContentType(ct);
     if (ct != null) {
       response.setContentTypeHeader(ct);
     }
   }
 }
Beispiel #2
0
 @Override
 public void processExistingResource(
     HttpManager manager, Request request, Response response, Resource resource)
     throws NotAuthorizedException, BadRequestException, ConflictException {
   try {
     org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
     // Prevent possibily of malicious clients using remote the parser to load remote resources
     builder.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
     org.jdom.Document doc = builder.build(request.getInputStream());
     String reportName = doc.getRootElement().getName();
     Report r = reports.get(reportName);
     if (r == null) {
       log.error("report not known: " + reportName);
       throw new BadRequestException(resource);
     } else {
       log.info("process report: " + reportName + " with : " + r.getClass());
       String xml = r.process(request.getHostHeader(), request.getAbsolutePath(), resource, doc);
       if (log.isTraceEnabled()) {
         log.trace("Report XML:\n" + xml);
       }
       response.setStatus(Response.Status.SC_MULTI_STATUS);
       response.setContentTypeHeader("text/xml");
       response.setEntity(new ByteArrayEntity(xml.getBytes("UTF-8")));
     }
   } catch (JDOMException ex) {
     java.util.logging.Logger.getLogger(ReportHandler.class.getName()).log(Level.SEVERE, null, ex);
   } catch (ReadingException ex) {
     throw new RuntimeException(ex);
   } catch (WritingException ex) {
     throw new RuntimeException(ex);
   } catch (IOException ex) {
     throw new RuntimeException(ex);
   }
 }
  @Override
  public String processForm(Map<String, String> parameters, Map<String, FileItem> files)
      throws BadRequestException, NotAuthorizedException, ConflictException {
    log.trace("process");
    try {
      ByteArrayOutputStream bout = new ByteArrayOutputStream();
      Request request = HttpManager.request();
      IOUtils.copy(request.getInputStream(), bout);
      String iCalText = bout.toString("UTF-8");
      if (log.isTraceEnabled()) {
        log.trace("Freebusy query: " + iCalText);
      }
      List<SchedulingResponseItem> respItems = queryFreeBusy(iCalText);

      String xml = schedulingHelper.generateXml(respItems);
      xmlResponse = xml.getBytes(StringUtils.UTF8);
      if (log.isTraceEnabled()) {
        log.trace("FreeBusy response= " + xml);
      }

    } catch (IOException ex) {
      throw new RuntimeException(ex);
    }
    return null;
  }
 private RootFolder getRootFolder(String host, Request req, boolean resolve) {
     RootFolder rootFolder = (RootFolder) req.getAttributes().get(ROOT_FOLDER_NAME);
     if (rootFolder == null && resolve) {
         rootFolder = resolve(host);
         req.getAttributes().put(ROOT_FOLDER_NAME, rootFolder);
     }
     return rootFolder;
 }
 @Override
 public RootFolder peekRootFolder() {
     Request req = HttpManager.request();
     if (req == null) {
         return null;
     }
     return getRootFolder(req.getHostHeader(), req, false);
 }
  @Override
  public void respondContent(
      Resource resource, Response response, Request request, Map<String, String> params)
      throws NotAuthorizedException, BadRequestException, NotFoundException {
    log.debug("respondContent: " + resource.getClass());
    Auth auth = request.getAuthorization();
    setRespondContentCommonHeaders(response, resource, auth);
    if (resource instanceof GetableResource) {
      GetableResource gr = (GetableResource) resource;
      String acc = request.getAcceptHeader();
      String ct = gr.getContentType(acc);
      if (ct != null) {
        ct = pickBestContentType(ct);
        response.setContentTypeHeader(ct);
      }
      cacheControlHelper.setCacheControl(gr, response, request.getAuthorization());

      Long contentLength = gr.getContentLength();
      Boolean doBuffering = null;
      if (resource instanceof BufferingControlResource) {
        BufferingControlResource bcr = (BufferingControlResource) resource;
        doBuffering = bcr.isBufferingRequired();
      }
      if (doBuffering == null) {
        if (buffering == null || buffering == BUFFERING.whenNeeded) {
          doBuffering =
              (contentLength
                  == null); // if no content length then we buffer content to find content length
        } else {
          doBuffering =
              (buffering
                  == BUFFERING
                      .always); // if not null or whenNeeded then buffering is explicitly enabled or
                                // disabled
        }
      }
      if (!doBuffering) {
        log.trace("sending content with known content length: " + contentLength);
        if (contentLength != null) {
          response.setContentLengthHeader(contentLength);
        }
        response.setEntity(new GetableResourceEntity(gr, params, ct));
      } else {
        BufferingGetableResourceEntity e =
            new BufferingGetableResourceEntity(gr, params, ct, contentLength, getMaxMemorySize());
        response.setEntity(e);
      }
    }
  }
 /**
  * The authentication result is written to a request attribute called "loginResult".
  *
  * <p>Its value is "true" if login succeeded and "false" if not. Note that a successful login does
  * not ensure that that authorisation will succeed.
  *
  * <p>If rendering a login page based on authentication and authorisation you should also look at
  * the "authReason" attribute set by the LoginResponseHandler which gives the reason for an
  * authorisation failure
  *
  * @param resource
  * @param request
  * @return
  */
 @Override
 public Object authenticate(Resource resource, Request request) {
   String userName = request.getParams().get(userNameParam);
   String pwd = request.getParams().get(passwordParam);
   Object o = resource.authenticate(userName, pwd);
   // set a request attribute that can be used when rendering
   if (o == null) {
     log.trace("Form authentication failed");
     request.getAttributes().put("loginResult", Boolean.FALSE);
   } else {
     log.trace("Form authentication succeeded");
     request.getAttributes().put("loginResult", Boolean.TRUE);
   }
   return o;
 }
 @Override
 public void respondNoContent(Resource resource, Response response, Request request) {
   //        log.debug( "respondNoContent" );
   // response.setStatus(Response.Status.SC_OK);
   // see comments in http://www.ettrema.com:8080/browse/MIL-87
   setRespondCommonHeaders(response, resource, Status.SC_NO_CONTENT, request.getAuthorization());
 }
 @Override
 public void respondWithOptions(
     Resource resource, Response response, Request request, List<String> methodsAllowed) {
   setRespondCommonHeaders(response, resource, Status.SC_OK, request.getAuthorization());
   response.setAllowHeader(methodsAllowed);
   response.setContentLengthHeader((long) 0);
 }
Beispiel #10
0
  protected void respondWithToken(LockToken tok, Request request, Response response) {
    response.setStatus(Status.SC_OK);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    XmlWriter writer = new XmlWriter(out);
    writer.writeXMLHeader();
    String d = WebDavProtocol.DAV_PREFIX;
    writer.open(d + ":prop  xmlns:" + d + "=\"DAV:\"");
    writer.newLine();
    writer.open(d + ":lockdiscovery");
    writer.newLine();
    writer.open(d + ":activelock");
    writer.newLine();
    lockWriterHelper.appendType(writer, tok.info.type);
    lockWriterHelper.appendScope(writer, tok.info.scope);
    lockWriterHelper.appendDepth(writer, tok.info.depth);
    lockWriterHelper.appendOwner(writer, tok.info.lockedByUser);
    lockWriterHelper.appendTimeout(writer, tok.timeout.getSeconds());
    lockWriterHelper.appendTokenId(writer, tok.tokenId);
    String url = PropFindPropertyBuilder.fixUrlForWindows(request.getAbsoluteUrl());
    lockWriterHelper.appendRoot(writer, url);
    writer.close(d + ":activelock");
    writer.close(d + ":lockdiscovery");
    writer.close(d + ":prop");
    writer.flush();

    LogUtils.debug(log, "lock response: ", out);
    response.setEntity(new ByteArrayEntity(out.toByteArray()));
    //        response.close();

  }
 @Override
 public Object authenticate(Resource resource, Request request) {
   log.debug("authenticate");
   Auth auth = request.getAuthorization();
   Object o = securityManager.authenticate(auth.getUser(), auth.getPassword());
   log.debug("result: " + o);
   return o;
 }
Beispiel #12
0
  @Override
  public void process(HttpManager manager, Request request, Response response)
      throws NotAuthorizedException, BadRequestException {
    if (!handlerHelper.checkExpects(responseHandler, request, response)) {
      return;
    }

    String host = request.getHostHeader();
    String url = HttpManager.decodeUrl(request.getAbsolutePath());

    // Find a resource if it exists
    Resource r = manager.getResourceFactory().getResource(host, url);
    if (r != null) {
      log.debug("locking existing resource: " + r.getName());
      processExistingResource(manager, request, response, r);
    } else {
      log.debug("lock target doesnt exist, attempting lock null..");
      processNonExistingResource(manager, request, response, host, url);
    }
  }
  @Override
  public boolean supports(Resource r, Request request) {
    Auth auth = request.getAuthorization();
    if (auth == null) {
      return false;
    }

    if (log.isTraceEnabled()) {
      log.trace("supports basic? requested scheme: " + auth.getScheme());
    }
    return auth.getScheme().equals(Scheme.BASIC);
  }
Beispiel #14
0
  protected void processExistingResource(
      HttpManager manager, Request request, Response response, Resource resource)
      throws NotAuthorizedException {
    if (handlerHelper.isNotCompatible(resource, request.getMethod()) || !isCompatible(resource)) {
      responseHandler.respondMethodNotImplemented(resource, response, request);
      return;
    }
    if (!handlerHelper.checkAuthorisation(manager, resource, request)) {
      responseHandler.respondUnauthorised(resource, response, request);
      return;
    }

    handlerHelper.checkExpects(responseHandler, request, response);

    LockableResource r = (LockableResource) resource;
    LockTimeout timeout = LockTimeout.parseTimeout(request);
    String ifHeader = request.getIfHeader();
    response.setContentTypeHeader(Response.XML);
    if (ifHeader == null || ifHeader.length() == 0) {
      processNewLock(manager, request, response, r, timeout);
    } else {
      processRefresh(manager, request, response, r, timeout, ifHeader);
    }
  }
 private ExternalIdentityProvider getSelectedIP(Request request) {
   List<ExternalIdentityProvider> list = authenticationService.getExternalIdentityProviders();
   if (list.size() == 1) {
     return list.get(0);
   } else {
     String ipName = request.getParams().get("_ip");
     if (ipName != null && ipName.length() > 0) {
       for (ExternalIdentityProvider eip : list) {
         if (ipName.equals(eip.getName())) {
           return eip;
         }
       }
     }
     return null;
   }
 }
Beispiel #16
0
  public static ReplicaInfo forRequest(Request request) {
    Map<String, String> parameters = request.getParams();

    if (parameters == null) {
      return EMPTY_INFO;
    }

    String r1 = parameters.get("r1");

    if (isNullOrEmpty(parameters.get("rid")) || isNullOrEmpty(r1) || r1.indexOf(',') == -1) {
      LOG.trace("returning empty QueryStringInfo for request");
      return EMPTY_INFO;
    } else {
      LOG.trace("returning non-empty QueryStringInfo for request");
      return new ReplicaInfo(request);
    }
  }
  @Override
  public void respondNotModified(GetableResource resource, Response response, Request request) {
    log.trace("respondNotModified");
    response.setStatus(Response.Status.SC_NOT_MODIFIED);
    response.setDateHeader(new Date());
    String etag = eTagGenerator.generateEtag(resource);
    if (etag != null) {
      response.setEtag(etag);
    }

    // Note that we use a simpler modified date handling here then when
    // responding with content, because in a not-modified situation the
    // modified date MUST be that of the actual resource
    Date modDate = resource.getModifiedDate();
    response.setLastModifiedHeader(modDate);

    cacheControlHelper.setCacheControl(resource, response, request.getAuthorization());
  }
 @Override
 public void respondPartialContent(
     GetableResource resource,
     Response response,
     Request request,
     Map<String, String> params,
     Range range)
     throws NotAuthorizedException, BadRequestException, NotFoundException {
   log.debug("respondPartialContent: " + range.getStart() + " - " + range.getFinish());
   response.setStatus(Response.Status.SC_PARTIAL_CONTENT);
   long st = range.getStart() == null ? 0 : range.getStart();
   long fn;
   Long cl = resource.getContentLength();
   if (range.getFinish() == null) {
     if (cl != null) {
       fn = cl.longValue() - 1; // position is one less then length
     } else {
       log.warn(
           "Couldnt calculate range end position because the resource is not reporting a content length, and no end position was requested by the client: "
               + resource.getName()
               + " - "
               + resource.getClass());
       fn = -1;
     }
   } else {
     fn = range.getFinish();
   }
   response.setContentRangeHeader(st, fn, cl);
   long contentLength = fn - st + 1;
   response.setContentLengthHeader(contentLength);
   response.setDateHeader(new Date());
   String etag = eTagGenerator.generateEtag(resource);
   if (etag != null) {
     response.setEtag(etag);
   }
   String acc = request.getAcceptHeader();
   String ct = resource.getContentType(acc);
   if (ct != null) {
     response.setContentTypeHeader(ct);
   }
   response.setEntity(new GetableResourceEntity(resource, range, params, ct));
 }
 @Override
 public boolean credentialsPresent(Request request) {
   return request.getAuthorization() != null;
 }
 @Override
 public void appendChallenges(Resource resource, Request request, List<String> challenges) {
   String realm = securityManager.getRealm(request.getHostHeader());
   challenges.add("Basic realm=\"" + realm + "\"");
 }
 @Override
 public boolean credentialsPresent(Request request) {
   return request.getParams() != null && request.getParams().containsKey(userNameParam);
 }
 private boolean isLogin(Request request) {
   return (request.getMethod() == Method.POST && request.getParams().get(userNameParam) != null);
 }
Beispiel #23
0
 private ReplicaInfo(Request request) {
   _parameters = request.getParams();
 }
 @Override
 public void respondCreated(Resource resource, Response response, Request request) {
   //        log.debug( "respondCreated" );
   setRespondCommonHeaders(response, resource, Status.SC_CREATED, request.getAuthorization());
 }