本文整理汇总了Java中org.apache.http.entity.ContentType.create方法的典型用法代码示例。如果您正苦于以下问题:Java ContentType.create方法的具体用法?Java ContentType.create怎么用?Java ContentType.create使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.entity.ContentType
的用法示例。
在下文中一共展示了ContentType.create方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setPostEntity
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Override
public void setPostEntity(File file, HttpPost httppost) {
httppost.setHeader("Content-Type", "application/octet-stream");
httppost.setHeader("Slug", file.getName());
HttpEntity e = new FileEntity(file, ContentType.create(MIME.getType(file)));
httppost.setEntity(e);
}
示例2: constructContentTypeObject
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
private ContentType constructContentTypeObject() {
ContentType _contentType = null;
if (contentType != null) {
if (charset != null) {
_contentType = ContentType.create(contentType, charset);
} else {
_contentType = ContentType.create(contentType);
}
}
return _contentType;
}
示例3: getEntity
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Override
protected HttpEntity getEntity(String path, Map<String, String> query, File localFile) throws IOException {
String extension = FileHelper.getExtension(path);
ContentType contentType = ContentType.create(getContentType(extension.toLowerCase()));
return new FileEntity(localFile, contentType);
}
示例4: getResponse
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Override
protected Response getResponse(String path, Map<String, String> queryStringMap) throws Exception {
if (queryStringMap.containsKey("folder")) {
String folder = queryStringMap.get("folder");
while (!folder.isEmpty() && folder.startsWith("/")) { // Remove the leading slashes if needed
folder = folder.substring(1);
}
String query = queryStringMap.get("query"); // Can be null
List<PhotatoFolder> folders = query == null ? this.photatoFilesManager.getFoldersInFolder(folder) : this.photatoFilesManager.searchFoldersInFolder(folder, query);
List<PhotatoMedia> medias = query == null ? this.photatoFilesManager.getMediasInFolder(folder) : this.photatoFilesManager.searchMediasInFolder(folder, query);
folders.sort((PhotatoFolder f1, PhotatoFolder f2) -> f1.filename.toLowerCase().compareTo(f2.filename.toLowerCase()));
medias.sort((PhotatoMedia m1, PhotatoMedia m2) -> {
int c = Long.compare(m1.timestamp, m2.timestamp);
if (c == 0) {
return m1.filename.toLowerCase().compareTo(m2.filename.toLowerCase());
} else {
return c;
}
});
FolderListResponse result = new FolderListResponse(folders, medias);
return new Response(HttpStatus.SC_OK, new StringEntity(SerialisationGsonBuilder.getGson().toJson(result), ContentType.create("application/json", "UTF-8")));
} else {
return PhotatoHandler.http404;
}
}
示例5: index
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
static Request index(IndexRequest indexRequest) {
String method = Strings.hasLength(indexRequest.id()) ? HttpPut.METHOD_NAME : HttpPost.METHOD_NAME;
boolean isCreate = (indexRequest.opType() == DocWriteRequest.OpType.CREATE);
String endpoint = endpoint(indexRequest.index(), indexRequest.type(), indexRequest.id(), isCreate ? "_create" : null);
Params parameters = Params.builder();
parameters.withRouting(indexRequest.routing());
parameters.withParent(indexRequest.parent());
parameters.withTimeout(indexRequest.timeout());
parameters.withVersion(indexRequest.version());
parameters.withVersionType(indexRequest.versionType());
parameters.withPipeline(indexRequest.getPipeline());
parameters.withRefreshPolicy(indexRequest.getRefreshPolicy());
parameters.withWaitForActiveShards(indexRequest.waitForActiveShards());
BytesRef source = indexRequest.source().toBytesRef();
ContentType contentType = ContentType.create(indexRequest.getContentType().mediaType());
HttpEntity entity = new ByteArrayEntity(source.bytes, source.offset, source.length, contentType);
return new Request(method, endpoint, parameters.getParams(), entity);
}
示例6: update
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
static Request update(UpdateRequest updateRequest) throws IOException {
String endpoint = endpoint(updateRequest.index(), updateRequest.type(), updateRequest.id(), "_update");
Params parameters = Params.builder();
parameters.withRouting(updateRequest.routing());
parameters.withParent(updateRequest.parent());
parameters.withTimeout(updateRequest.timeout());
parameters.withRefreshPolicy(updateRequest.getRefreshPolicy());
parameters.withWaitForActiveShards(updateRequest.waitForActiveShards());
parameters.withDocAsUpsert(updateRequest.docAsUpsert());
parameters.withFetchSourceContext(updateRequest.fetchSource());
parameters.withRetryOnConflict(updateRequest.retryOnConflict());
parameters.withVersion(updateRequest.version());
parameters.withVersionType(updateRequest.versionType());
// The Java API allows update requests with different content types
// set for the partial document and the upsert document. This client
// only accepts update requests that have the same content types set
// for both doc and upsert.
XContentType xContentType = null;
if (updateRequest.doc() != null) {
xContentType = updateRequest.doc().getContentType();
}
if (updateRequest.upsertRequest() != null) {
XContentType upsertContentType = updateRequest.upsertRequest().getContentType();
if ((xContentType != null) && (xContentType != upsertContentType)) {
throw new IllegalStateException("Update request cannot have different content types for doc [" + xContentType + "]" +
" and upsert [" + upsertContentType + "] documents");
} else {
xContentType = upsertContentType;
}
}
if (xContentType == null) {
xContentType = Requests.INDEX_CONTENT_TYPE;
}
BytesRef source = XContentHelper.toXContent(updateRequest, xContentType, false).toBytesRef();
HttpEntity entity = new ByteArrayEntity(source.bytes, source.offset, source.length, ContentType.create(xContentType.mediaType()));
return new Request(HttpPost.METHOD_NAME, endpoint, parameters.getParams(), entity);
}
示例7: sendMail
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
private void sendMail(String emailFrom, String txtMessage, String subject,
boolean appendLog) throws IOException {
try (CloseableHttpClient httpClient = HttpClients.createDefault();) {
HttpPost uploadFile = new HttpPost(URL);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
StringBuilder sbMessage = new StringBuilder();
sbMessage.append(txtMessage);
sbMessage.append("\n\n------pgCodeKeeper configuration--------\n"); //$NON-NLS-1$
appendCodeKeeperPluginsInformation(sbMessage);
ContentType utf = ContentType.create(TEXT_PLAIN, StandardCharsets.UTF_8);
builder.addTextBody(POST_SUBJECT, subject, utf);
builder.addTextBody(POST_EMAIL, emailFrom, utf);
builder.addTextBody(POST_BODY, sbMessage.toString(), utf);
if (appendLog) {
Path log = Platform.getLogFileLocation().toFile().toPath();
byte[] logBytes;
try {
logBytes = Files.readAllBytes(log);
} catch (NoSuchFileException ex) {
logBytes = ex.toString().getBytes(StandardCharsets.UTF_8);
}
builder.addBinaryBody(POST_FILES, logBytes,
ContentType.APPLICATION_OCTET_STREAM,
log.getFileName().toString());
}
HttpEntity multipart = builder.build();
uploadFile.setEntity(multipart);
try (CloseableHttpResponse response = httpClient.execute(uploadFile);) {
HttpEntity responseEntity = response.getEntity();
if (responseEntity == null) {
throw new IOException(MessageFormat.format(
Messages.FeedBackDialog_bad_response, response.toString()));
}
String entity = EntityUtils.toString(responseEntity);
if (!STATUS_OK.equals(PATTERN_WS.matcher(entity).replaceAll(""))) { //$NON-NLS-1$
throw new IOException(MessageFormat.format(
Messages.FeedBackDialog_bad_response, entity));
}
}
}
}
示例8: getStringBody
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
private static ContentBody getStringBody(String value) {
ContentType contentType = ContentType.create("text/plain", Consts.UTF_8);
return new StringBody(value, contentType);
}
示例9: getResponse
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Override
protected Response getResponse(String path, Map<String, String> query) throws Exception {
return new Response(200, new FileEntity(folderRoot.resolve("index.html").toFile(), ContentType.create("text/html; charset=UTF-8")));
}
示例10: getEntity
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
protected HttpEntity getEntity(String path, Map<String, String> query, File localFile) throws IOException {
String extension = FileHelper.getExtension(path);
return new FileEntity(localFile, ContentType.create(getContentType(extension.toLowerCase())));
}
示例11: InputStreamBodyWithLength
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
public InputStreamBodyWithLength(String resourcePath, String contentType, String fileName) throws ClientException {
super(ResourceUtil.getResourceAsStream(resourcePath), ContentType.create(contentType), fileName);
this.streamLength = getResourceStreamLength(resourcePath);
}
示例12: entities
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
@Test
public void entities() throws IOException {
StringEntity entity = new StringEntity("important message", Consts.UTF_8);
EntityUtils.toString(entity);
EntityUtils.toByteArray(entity);
StringBuilder sb = new StringBuilder();
Map<String, String> env = System.getenv();
for (Map.Entry<String, String> envEntry : env.entrySet()) {
sb.append(envEntry.getKey())
.append(": ").append(envEntry.getValue())
.append("\r\n");
}
// construct without a character encoding (defaults to ISO-8859-1)
HttpEntity myEntity1 = new StringEntity(sb.toString());
// alternatively construct with an encoding (mime type defaults to "text/plain")
HttpEntity myEntity2 = new StringEntity(sb.toString(), Consts.UTF_8);
// alternatively construct with an encoding and a mime type
HttpEntity myEntity3 = new StringEntity(sb.toString(),
ContentType.create("text/plain", Consts.UTF_8));
BasicHttpEntity myEntity = new BasicHttpEntity();
myEntity.setContent(new FileInputStream(""));
myEntity.setContentLength(340); // sets the length to 340
new ByteArrayEntity(new byte[] {1,2,3},
ContentType.APPLICATION_OCTET_STREAM);
new FileEntity(new File(""),
ContentType.create("application/java-archive"));
}
示例13: UrlEncodedFormEntity
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* Constructs a new {@link UrlEncodedFormEntity} with the list
* of parameters in the specified encoding.
*
* @param parameters list of name/value pairs
* @param charset encoding the name/value pairs be encoded with
* @throws UnsupportedEncodingException if the encoding isn't supported
*/
public UrlEncodedFormEntity (
final List <? extends NameValuePair> parameters,
final String charset) throws UnsupportedEncodingException {
super(URLEncodedUtils.format(parameters,
charset != null ? charset : HTTP.DEF_CONTENT_CHARSET.name()),
ContentType.create(URLEncodedUtils.CONTENT_TYPE, charset));
}
示例14: FileExposingFileEntity
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* @param pFile -
* @param pContentType -
*/
public FileExposingFileEntity(File pFile, String pContentType) {
super(pFile, ContentType.create(pContentType));
}
示例15: setRequestBody
import org.apache.http.entity.ContentType; //导入方法依赖的package包/类
/**
* Set the request body using content in a file.
*
* @param file The file containing the request
* @param contentType The content type e.g. 'text/xml'
*/
@PublicAtsApi
public void setRequestBody( File file, String contentType ) {
requestBody = new FileEntity(file, ContentType.create(contentType));
}