示例#1
0
  /**
   * Post a file to the server. The different elements of the post are encoded in the specified
   * message.
   *
   * @param message The message that contains the post information.
   * @throws SWORDClientException if there is an error during the post operation.
   */
  public DepositResponse postFile(PostMessage message) throws SWORDClientException {
    if (message == null) {
      throw new SWORDClientException("Message cannot be null.");
    }

    PostMethod httppost = new PostMethod(message.getDestination());

    if (doAuthentication) {
      setBasicCredentials(username, password);
      httppost.setDoAuthentication(true);
    }

    DepositResponse response = null;

    String messageBody = "";

    try {
      if (message.isUseMD5()) {
        String md5 = ChecksumUtils.generateMD5(message.getFilepath());
        if (message.getChecksumError()) {
          md5 = "1234567890";
        }
        log.debug("checksum error is: " + md5);
        if (md5 != null) {
          httppost.addRequestHeader(new Header(HttpHeaders.CONTENT_MD5, md5));
        }
      }

      String filename = message.getFilename();
      if (!"".equals(filename)) {
        httppost.addRequestHeader(
            new Header(HttpHeaders.CONTENT_DISPOSITION, " filename=" + filename));
      }

      if (containsValue(message.getSlug())) {
        httppost.addRequestHeader(new Header(HttpHeaders.SLUG, message.getSlug()));
      }

      if (message.getCorruptRequest()) {
        // insert a header with an invalid boolean value
        httppost.addRequestHeader(new Header(HttpHeaders.X_NO_OP, "Wibble"));
      } else {
        httppost.addRequestHeader(
            new Header(HttpHeaders.X_NO_OP, Boolean.toString(message.isNoOp())));
      }
      httppost.addRequestHeader(
          new Header(HttpHeaders.X_VERBOSE, Boolean.toString(message.isVerbose())));

      String packaging = message.getPackaging();
      if (packaging != null && packaging.length() > 0) {
        httppost.addRequestHeader(new Header(HttpHeaders.X_PACKAGING, packaging));
      }

      String onBehalfOf = message.getOnBehalfOf();
      if (containsValue(onBehalfOf)) {
        httppost.addRequestHeader(new Header(HttpHeaders.X_ON_BEHALF_OF, onBehalfOf));
      }

      String userAgent = message.getUserAgent();
      if (containsValue(userAgent)) {
        httppost.addRequestHeader(new Header(HttpHeaders.USER_AGENT, userAgent));
      }

      FileRequestEntity requestEntity =
          new FileRequestEntity(new File(message.getFilepath()), message.getFiletype());
      httppost.setRequestEntity(requestEntity);

      client.executeMethod(httppost);
      status = new Status(httppost.getStatusCode(), httppost.getStatusText());

      log.info("Checking the status code: " + status.getCode());

      if (status.getCode() == HttpStatus.SC_ACCEPTED || status.getCode() == HttpStatus.SC_CREATED) {
        messageBody = readResponse(httppost.getResponseBodyAsStream());
        response = new DepositResponse(status.getCode());
        response.setLocation(httppost.getResponseHeader("Location").getValue());
        // added call for the status code.
        lastUnmarshallInfo = response.unmarshall(messageBody, new Properties());
      } else {
        messageBody = readResponse(httppost.getResponseBodyAsStream());
        response = new DepositResponse(status.getCode());
        response.unmarshallErrorDocument(messageBody);
      }
      return response;

    } catch (NoSuchAlgorithmException nex) {
      throw new SWORDClientException("Unable to use MD5. " + nex.getMessage(), nex);
    } catch (HttpException ex) {
      throw new SWORDClientException(ex.getMessage(), ex);
    } catch (IOException ioex) {
      throw new SWORDClientException(ioex.getMessage(), ioex);
    } catch (UnmarshallException uex) {
      throw new SWORDClientException(uex.getMessage() + "(<pre>" + messageBody + "</pre>)", uex);
    } finally {
      httppost.releaseConnection();
    }
  }
示例#2
0
  /**
   * Retrieve the service document. The service document is located at the specified URL. This calls
   * getServiceDocument(url,onBehalfOf).
   *
   * @param url The location of the service document.
   * @return The ServiceDocument, or <code>null</code> if there was a problem accessing the
   *     document. e.g. invalid access.
   * @throws SWORDClientException If there is an error accessing the resource.
   */
  public ServiceDocument getServiceDocument(String url, String onBehalfOf)
      throws SWORDClientException {
    URL serviceDocURL = null;
    try {
      serviceDocURL = new URL(url);
    } catch (MalformedURLException e) {
      // Try relative URL
      URL baseURL = null;
      try {
        baseURL = new URL("http", server, Integer.valueOf(port), "/");
        serviceDocURL = new URL(baseURL, (url == null) ? "" : url);
      } catch (MalformedURLException e1) {
        // No dice, can't even form base URL...
        throw new SWORDClientException(
            url
                + " is not a valid URL ("
                + e1.getMessage()
                + "), and could not form a relative one from: "
                + baseURL
                + " / "
                + url,
            e1);
      }
    }

    GetMethod httpget = new GetMethod(serviceDocURL.toExternalForm());
    if (doAuthentication) {
      // this does not perform any check on the username password. It
      // relies on the server to determine if the values are correct.
      setBasicCredentials(username, password);
      httpget.setDoAuthentication(true);
    }

    Properties properties = new Properties();

    if (containsValue(onBehalfOf)) {
      log.debug("Setting on-behalf-of: " + onBehalfOf);
      httpget.addRequestHeader(new Header(HttpHeaders.X_ON_BEHALF_OF, onBehalfOf));
      properties.put(HttpHeaders.X_ON_BEHALF_OF, onBehalfOf);
    }

    if (containsValue(userAgent)) {
      log.debug("Setting userAgent: " + userAgent);
      httpget.addRequestHeader(new Header(HttpHeaders.USER_AGENT, userAgent));
      properties.put(HttpHeaders.USER_AGENT, userAgent);
    }

    ServiceDocument doc = null;

    try {
      client.executeMethod(httpget);
      // store the status code
      status = new Status(httpget.getStatusCode(), httpget.getStatusText());

      if (status.getCode() == HttpStatus.SC_OK) {
        String message = readResponse(httpget.getResponseBodyAsStream());
        log.debug("returned message is: " + message);
        doc = new ServiceDocument();
        lastUnmarshallInfo = doc.unmarshall(message, properties);
      } else {
        throw new SWORDClientException("Received error from service document request: " + status);
      }
    } catch (HttpException ex) {
      throw new SWORDClientException(ex.getMessage(), ex);
    } catch (IOException ioex) {
      throw new SWORDClientException(ioex.getMessage(), ioex);
    } catch (UnmarshallException uex) {
      throw new SWORDClientException(uex.getMessage(), uex);
    } finally {
      httpget.releaseConnection();
    }

    return doc;
  }