Exemplo n.º 1
0
  /** Using the PostMethod */
  public static String getCharsetWithMethod(String url) {
    PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    String charset = post.getResponseCharSet();
    System.out.println(charset);

    return charset;
  }
  /** Upload to an file node structure, see SLING-168 */
  public void uploadToFileNode(String url, File localFile, String fieldName, String typeHint)
      throws IOException {

    final Part[] parts = new Part[typeHint == null ? 1 : 2];
    parts[0] = new FilePart(fieldName, localFile);
    if (typeHint != null) {
      parts[1] = new StringPart(fieldName + "@TypeHint", typeHint);
    }
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

    final int status = httpClient.executeMethod(post);
    if (status != 200) { // fmeschbe: The default sling status is 200, not 302
      throw new HttpStatusCodeException(200, status, "POST", url);
    }
  }
  /** Upload multiple files to file node structures */
  public void uploadToFileNodes(
      String url, File[] localFiles, String[] fieldNames, String[] typeHints) throws IOException {

    List<Part> partsList = new ArrayList<Part>();
    for (int i = 0; i < localFiles.length; i++) {
      Part filePart = new FilePart(fieldNames[i], localFiles[i]);
      partsList.add(filePart);
      if (typeHints != null) {
        Part typeHintPart = new StringPart(fieldNames[i] + "@TypeHint", typeHints[i]);
        partsList.add(typeHintPart);
      }
    }

    final Part[] parts = partsList.toArray(new Part[partsList.size()]);
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);
    post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));

    final int status = httpClient.executeMethod(post);
    if (status != 200) { // fmeschbe: The default sling status is 200, not 302
      throw new HttpStatusCodeException(200, status, "POST", url);
    }
  }
  /**
   * Create a node under given path, using a POST to Sling
   *
   * @param url under which node is created
   * @param multiPart if true, does a multipart POST
   * @return the URL that Sling provides to display the node
   */
  public String createNode(
      String url,
      Map<String, String> clientNodeProperties,
      Map<String, String> requestHeaders,
      boolean multiPart)
      throws IOException {
    final PostMethod post = new PostMethod(url);
    post.setFollowRedirects(false);

    // create a private copy of the properties to not tamper with
    // the properties of the client
    Map<String, String> nodeProperties = new HashMap<String, String>();

    // add sling specific properties
    nodeProperties.put(":redirect", "*");
    nodeProperties.put(":displayExtension", "");
    nodeProperties.put(":status", "browser");

    // take over any client provided properties
    if (clientNodeProperties != null) {
      nodeProperties.putAll(clientNodeProperties);
    } else {
      // add fake property - otherwise the node is not created
      nodeProperties.put("jcr:created", "");
    }

    // force form encoding to UTF-8, which is what we use to convert the
    // string parts into stream data
    nodeProperties.put("_charset_", "UTF-8");

    if (nodeProperties.size() > 0) {
      if (multiPart) {
        final List<Part> partList = new ArrayList<Part>();
        for (Map.Entry<String, String> e : nodeProperties.entrySet()) {
          if (e.getValue() != null) {
            partList.add(new StringPart(e.getKey().toString(), e.getValue().toString(), "UTF-8"));
          }
        }
        final Part[] parts = partList.toArray(new Part[partList.size()]);
        post.setRequestEntity(new MultipartRequestEntity(parts, post.getParams()));
      } else {
        for (Map.Entry<String, String> e : nodeProperties.entrySet()) {
          post.addParameter(e.getKey(), e.getValue());
        }
      }
    }

    if (requestHeaders != null) {
      for (Map.Entry<String, String> e : requestHeaders.entrySet()) {
        post.addRequestHeader(e.getKey(), e.getValue());
      }
    }

    final int status = httpClient.executeMethod(post);
    if (status != 302) {
      throw new HttpStatusCodeException(302, status, "POST", url);
    }
    String location = post.getResponseHeader("Location").getValue();
    post.releaseConnection();
    // simple check if host is missing
    if (!location.startsWith("http://")) {
      String host = HttpTestBase.HTTP_BASE_URL;
      int idx = host.indexOf('/', 8);
      if (idx > 0) {
        host = host.substring(0, idx);
      }
      location = host + location;
    }
    return location;
  }