Java Code Examples for org.apache.commons.httpclient.HttpStatus#SC_CREATED

The following examples show how to use org.apache.commons.httpclient.HttpStatus#SC_CREATED . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public void moveFolder(String folderPath, String targetPath) throws IOException {
    MoveMethod method = new MoveMethod(URIUtil.encodePath(getFolderPath(folderPath)),
            URIUtil.encodePath(getFolderPath(targetPath)), false);
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
            throw new HttpPreconditionFailedException(BundleMessage.format("EXCEPTION_UNABLE_TO_MOVE_FOLDER"));
        } else if (statusCode != HttpStatus.SC_CREATED) {
            throw DavGatewayHttpClientFacade.buildHttpResponseException(method);
        } else if (folderPath.equalsIgnoreCase("/users/" + getEmail() + "/calendar")) {
            // calendar renamed, need to reload well known folders 
            getWellKnownFolders();
        }
    } finally {
        method.releaseConnection();
    }
}
 
Example 2
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
protected void moveMessage(String sourceUrl, String targetFolder) throws IOException {
    String targetPath = URIUtil.encodePath(getFolderPath(targetFolder)) + '/' + UUID.randomUUID().toString();
    MoveMethod method = new MoveMethod(URIUtil.encodePath(sourceUrl), targetPath, false);
    // allow rename if a message with the same name exists
    method.addRequestHeader("Allow-Rename", "t");
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
            throw new DavMailException("EXCEPTION_UNABLE_TO_MOVE_MESSAGE");
        } else if (statusCode != HttpStatus.SC_CREATED) {
            throw DavGatewayHttpClientFacade.buildHttpResponseException(method);
        }
    } finally {
        method.releaseConnection();
    }
}
 
Example 3
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
protected void copyMessage(String sourceUrl, String targetFolder) throws IOException {
    String targetPath = URIUtil.encodePath(getFolderPath(targetFolder)) + '/' + UUID.randomUUID().toString();
    CopyMethod method = new CopyMethod(URIUtil.encodePath(sourceUrl), targetPath, false);
    // allow rename if a message with the same name exists
    method.addRequestHeader("Allow-Rename", "t");
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
            throw new DavMailException("EXCEPTION_UNABLE_TO_COPY_MESSAGE");
        } else if (statusCode != HttpStatus.SC_CREATED) {
            throw DavGatewayHttpClientFacade.buildHttpResponseException(method);
        }
    } finally {
        method.releaseConnection();
    }
}
 
Example 4
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected void moveToTrash(ExchangeSession.Message message) throws IOException {
    String destination = URIUtil.encodePath(deleteditemsUrl) + '/' + UUID.randomUUID().toString();
    LOGGER.debug("Deleting : " + message.permanentUrl + " to " + destination);
    MoveMethod method = new MoveMethod(encodeAndFixUrl(message.permanentUrl), destination, false);
    method.addRequestHeader("Allow-rename", "t");

    int status = DavGatewayHttpClientFacade.executeHttpMethod(httpClient, method);
    // do not throw error if already deleted
    if (status != HttpStatus.SC_CREATED && status != HttpStatus.SC_NOT_FOUND) {
        throw DavGatewayHttpClientFacade.buildHttpResponseException(method);
    }
    if (method.getResponseHeader("Location") != null) {
        destination = method.getResponseHeader("Location").getValue();
    }

    LOGGER.debug("Deleted to :" + destination);
}
 
Example 5
Source File: EwsExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public int createFolder(String folderPath, String folderClass, Map<String, String> properties) throws IOException {
    FolderPath path = new FolderPath(folderPath);
    EWSMethod.Item folder = new EWSMethod.Item();
    folder.type = "Folder";
    folder.put("FolderClass", folderClass);
    folder.put("DisplayName", decodeFolderName(path.folderName));
    // TODO: handle properties
    CreateFolderMethod createFolderMethod = new CreateFolderMethod(getFolderId(path.parentPath), folder);
    executeMethod(createFolderMethod);
    return HttpStatus.SC_CREATED;
}
 
Example 6
Source File: EwsExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * @inheritDoc
 */
@Override
public int updateFolder(String folderPath, Map<String, String> properties) throws IOException {
    ArrayList<FieldUpdate> updates = new ArrayList<>();
    for (Map.Entry<String, String> entry : properties.entrySet()) {
        updates.add(new FieldUpdate(Field.get(entry.getKey()), entry.getValue()));
    }
    UpdateFolderMethod updateFolderMethod = new UpdateFolderMethod(internalGetFolder(folderPath).folderId, updates);

    executeMethod(updateFolderMethod);
    return HttpStatus.SC_CREATED;
}
 
Example 7
Source File: ExchangeDavMethod.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Return method http status code.
 *
 * @return http status code
 * @throws HttpException on error
 */
public int getResponseStatusCode() throws HttpException {
    String responseDescription = getResponse().getResponseDescription();
    if ("HTTP/1.1 201 Created".equals(responseDescription)) {
        return HttpStatus.SC_CREATED;
    } else {
        return HttpStatus.SC_OK;
    }
}
 
Example 8
Source File: DavExchangeSession.java    From davmail with GNU General Public License v2.0 5 votes vote down vote up
protected void moveItem(MoveMethod method) throws IOException {
    try {
        int statusCode = httpClient.executeMethod(method);
        if (statusCode == HttpStatus.SC_PRECONDITION_FAILED) {
            throw new DavMailException("EXCEPTION_UNABLE_TO_MOVE_ITEM");
        } else if (statusCode != HttpStatus.SC_CREATED && statusCode != HttpStatus.SC_OK) {
            throw DavGatewayHttpClientFacade.buildHttpResponseException(method);
        }
    } finally {
        method.releaseConnection();
    }
}
 
Example 9
Source File: OAuth2Client.java    From Knowage-Server with GNU Affero General Public License v3.0 4 votes vote down vote up
public String getToken(String id, String password) {
	logger.debug("IN");
	try {
		HttpClient client = getHttpClient();
		String url = config.getProperty("REST_BASE_URL") + config.getProperty("TOKEN_PATH");
		PostMethod httppost = new PostMethod(url);
		httppost.setRequestHeader("Content-Type", "application/json");

		String body = "{\r\n";
		body += "    \"auth\": {\r\n";
		body += "        \"identity\": {\r\n";
		body += "            \"methods\": [\r\n";
		body += "                \"password\"\r\n";
		body += "            ],\r\n";
		body += "            \"password\": {\r\n";
		body += "                \"user\": {\r\n";
		body += "                    \"id\": \"" + id + "\",\r\n";
		body += "                    \"password\": \"" + password + "\"\r\n";
		body += "                }\r\n";
		body += "            }\r\n";
		body += "        }\r\n";
		body += "    }\r\n";
		body += "}";

		httppost.setRequestBody(body);

		int statusCode = client.executeMethod(httppost);

		if (statusCode != HttpStatus.SC_CREATED) {
			logger.error("Error while getting access token from IdM REST API: server returned statusCode = " + statusCode);

			byte[] response = httppost.getResponseBody();
			LogMF.error(logger, "Server response is:\n{0}", new Object[] { new String(response) });

			throw new SpagoBIRuntimeException("Error while getting access token from IdM REST API: server returned statusCode = " + statusCode);
		}

		return httppost.getResponseHeader("X-Subject-Token").getValue();
	} catch (Exception e) {
		throw new SpagoBIRuntimeException("Error while trying to get token from IdM REST API", e);
	} finally {
		logger.debug("OUT");
	}
}
 
Example 10
Source File: KafkaAvroSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Register a schema to the Kafka schema registry
 *
 * @param schema
 * @return schema ID of the registered schema
 * @throws SchemaRegistryException if registration failed
 */
@Override
public synchronized String register(Schema schema) throws SchemaRegistryException {

  // Change namespace if override specified
  if (this.namespaceOverride.isPresent()) {
    schema = AvroUtils.switchNamespace(schema, this.namespaceOverride.get());
  }

  LOG.info("Registering schema " + schema.toString());

  PostMethod post = new PostMethod(url);
  post.addParameter("schema", schema.toString());

  HttpClient httpClient = this.borrowClient();
  try {
    LOG.debug("Loading: " + post.getURI());
    int statusCode = httpClient.executeMethod(post);
    if (statusCode != HttpStatus.SC_CREATED) {
      throw new SchemaRegistryException("Error occurred while trying to register schema: " + statusCode);
    }

    String response;
    response = post.getResponseBodyAsString();
    if (response != null) {
      LOG.info("Received response " + response);
    }

    String schemaKey;
    Header[] headers = post.getResponseHeaders(SCHEMA_ID_HEADER_NAME);
    if (headers.length != 1) {
      throw new SchemaRegistryException(
          "Error reading schema id returned by registerSchema call: headers.length = " + headers.length);
    } else if (!headers[0].getValue().startsWith(SCHEMA_ID_HEADER_PREFIX)) {
      throw new SchemaRegistryException(
          "Error parsing schema id returned by registerSchema call: header = " + headers[0].getValue());
    } else {
      LOG.info("Registered schema successfully");
      schemaKey = headers[0].getValue().substring(SCHEMA_ID_HEADER_PREFIX.length());
    }

    return schemaKey;
  } catch (Throwable t) {
    throw new SchemaRegistryException(t);
  } finally {
    post.releaseConnection();
    this.httpClientPool.returnObject(httpClient);
  }
}
 
Example 11
Source File: LiKafkaSchemaRegistry.java    From incubator-gobblin with Apache License 2.0 4 votes vote down vote up
/**
 * Register a schema to the Kafka schema registry
 *
 * @param schema
 * @param post
 * @return schema ID of the registered schema
 * @throws SchemaRegistryException if registration failed
 */
public synchronized MD5Digest register(Schema schema, PostMethod post) throws SchemaRegistryException {

  // Change namespace if override specified
  if (this.namespaceOverride.isPresent()) {
    schema = AvroUtils.switchNamespace(schema, this.namespaceOverride.get());
  }

  LOG.info("Registering schema " + schema.toString());

  post.addParameter("schema", schema.toString());

  HttpClient httpClient = this.borrowClient();
  try {
    LOG.debug("Loading: " + post.getURI());
    int statusCode = httpClient.executeMethod(post);
    if (statusCode != HttpStatus.SC_CREATED) {
      throw new SchemaRegistryException("Error occurred while trying to register schema: " + statusCode);
    }

    String response;
    response = post.getResponseBodyAsString();
    if (response != null) {
      LOG.info("Received response " + response);
    }

    String schemaKey;
    Header[] headers = post.getResponseHeaders(SCHEMA_ID_HEADER_NAME);
    if (headers.length != 1) {
      throw new SchemaRegistryException(
          "Error reading schema id returned by registerSchema call: headers.length = " + headers.length);
    } else if (!headers[0].getValue().startsWith(SCHEMA_ID_HEADER_PREFIX)) {
      throw new SchemaRegistryException(
          "Error parsing schema id returned by registerSchema call: header = " + headers[0].getValue());
    } else {
      LOG.info("Registered schema successfully");
      schemaKey = headers[0].getValue().substring(SCHEMA_ID_HEADER_PREFIX.length());
    }
    MD5Digest schemaId = MD5Digest.fromString(schemaKey);
    return schemaId;
  } catch (Throwable t) {
    throw new SchemaRegistryException(t);
  } finally {
    post.releaseConnection();
    this.httpClientPool.returnObject(httpClient);
  }
}