本文整理汇总了Java中org.apache.http.entity.ContentType.TEXT_PLAIN属性的典型用法代码示例。如果您正苦于以下问题:Java ContentType.TEXT_PLAIN属性的具体用法?Java ContentType.TEXT_PLAIN怎么用?Java ContentType.TEXT_PLAIN使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类org.apache.http.entity.ContentType
的用法示例。
在下文中一共展示了ContentType.TEXT_PLAIN属性的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: constructContentBody
ContentBody constructContentBody() throws UnsupportedEncodingException {
ContentType contentTypeObject = constructContentTypeObject();
if (filePath != null) { // FILE part
if (contentTypeObject != null) {
return new FileBody(new File(filePath), contentTypeObject);
} else {
return new FileBody(new File(filePath));
}
} else if (content != null) { // TEXT part
if (contentTypeObject != null) {
return new StringBody(content, contentTypeObject);
} else {
return new StringBody(content, ContentType.TEXT_PLAIN);
}
} else { // BYTE ARRAY part
if (contentTypeObject != null) {
return new ByteArrayBody(this.fileBytes, contentTypeObject, fileName);
} else {
return new ByteArrayBody(this.fileBytes, fileName);
}
}
}
示例2: attachFileToTransaction
/**
* Retorna a lista de anexos de uma transação com ou sem filtro de tipo
* @param idTransacao: Código da transação à ser consultada
* @param arquivo: Arquivo à ser anexado (imagem [JPEG/PNG] ou documento [PDF])
* @param tipoAnexo: Tipo de anexo à ser enviado
* @return boolean
*/
public boolean attachFileToTransaction(String idTransacao, File arquivo, TipoAnexo tipoAnexo) throws IOException,
PJBankException {
Set<String> extensoesPermitidas = new HashSet<>();
extensoesPermitidas.add("pdf");
extensoesPermitidas.add("jpg");
extensoesPermitidas.add("jpeg");
extensoesPermitidas.add("png");
if (!extensoesPermitidas.contains(FilenameUtils.getExtension(arquivo.getName()))) {
throw new IllegalArgumentException("O arquivo a ser anexado em uma transação deve estar no formato PDF, JPG," +
" JPEG ou PNG, sendo assim um documento ou uma imagem.");
}
PJBankClient client = new PJBankClient(this.endPoint.concat("/transacoes/").concat(idTransacao).concat("/documentos"));
HttpPost httpPost = client.getHttpPostClient();
httpPost.addHeader("x-chave-conta", this.chave);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
//FileBody fileBody = new FileBody(arquivo);
StringBody stringBody = new StringBody(tipoAnexo.getName(), ContentType.TEXT_PLAIN);
//builder.addPart("arquivos", fileBody);
builder.addBinaryBody("arquivos", arquivo,
ContentType.APPLICATION_OCTET_STREAM, arquivo.getName());
builder.addPart("tipo", stringBody);
httpPost.setEntity(builder.build());
return client.doRequest(httpPost).getStatusLine().getStatusCode() == 201;
}
示例3: testWrapExceptionToPreserveStatus
public void testWrapExceptionToPreserveStatus() throws IOException {
Exception cause = new Exception();
// Successfully get the status without a body
RestStatus status = randomFrom(RestStatus.values());
ElasticsearchStatusException wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), null, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("No error body.", wrapped.getMessage());
// Successfully get the status without a body
HttpEntity okEntity = new StringEntity("test body", ContentType.TEXT_PLAIN);
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), okEntity, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("body=test body", wrapped.getMessage());
// Successfully get the status with a broken body
IOException badEntityException = new IOException();
HttpEntity badEntity = mock(HttpEntity.class);
when(badEntity.getContent()).thenThrow(badEntityException);
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(status.getStatus(), badEntity, cause);
assertEquals(status, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Failed to extract body.", wrapped.getMessage());
assertEquals(badEntityException, wrapped.getSuppressed()[0]);
// Fail to get the status without a body
int notAnHttpStatus = -1;
assertNull(RestStatus.fromCode(notAnHttpStatus));
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, null, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. No error body.", wrapped.getMessage());
// Fail to get the status without a body
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, okEntity, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. body=test body", wrapped.getMessage());
// Fail to get the status with a broken body
wrapped = RemoteScrollableHitSource.wrapExceptionToPreserveStatus(notAnHttpStatus, badEntity, cause);
assertEquals(RestStatus.INTERNAL_SERVER_ERROR, wrapped.status());
assertEquals(cause, wrapped.getCause());
assertEquals("Couldn't extract status [" + notAnHttpStatus + "]. Failed to extract body.", wrapped.getMessage());
assertEquals(badEntityException, wrapped.getSuppressed()[0]);
}