本文整理汇总了Java中com.epam.reportportal.apache.http.entity.ContentType类的典型用法代码示例。如果您正苦于以下问题:Java ContentType类的具体用法?Java ContentType怎么用?Java ContentType使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
ContentType类属于com.epam.reportportal.apache.http.entity包,在下文中一共展示了ContentType类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parse
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* Returns a list of {@link NameValuePair NameValuePairs} as parsed from an {@link HttpEntity}. The encoding is
* taken from the entity's Content-Encoding header.
* <p>
* This is typically used while parsing an HTTP POST.
*
* @param entity
* The entity to parse
* @return a list of {@link NameValuePair} as built from the URI's query portion.
* @throws IOException
* If there was an exception getting the entity's data.
*/
public static List <NameValuePair> parse(
final HttpEntity entity) throws IOException {
final ContentType contentType = ContentType.get(entity);
if (contentType != null && contentType.getMimeType().equalsIgnoreCase(CONTENT_TYPE)) {
final String content = EntityUtils.toString(entity, Consts.ASCII);
if (content != null && content.length() > 0) {
Charset charset = contentType.getCharset();
if (charset == null) {
charset = HTTP.DEF_CONTENT_CHARSET;
}
return parse(content, charset, QP_SEPS);
}
}
return Collections.emptyList();
}
示例2: testDefaultContent
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testDefaultContent() throws Exception {
final String s = "Message content";
StringEntity httpentity = new StringEntity(s, ContentType.create("text/csv", "ANSI_X3.4-1968"));
Assert.assertEquals("text/csv; charset=US-ASCII",
httpentity.getContentType().getValue());
httpentity = new StringEntity(s, Consts.ASCII.name());
Assert.assertEquals("text/plain; charset=US-ASCII",
httpentity.getContentType().getValue());
httpentity = new StringEntity(s, Consts.ASCII);
Assert.assertEquals("text/plain; charset=US-ASCII",
httpentity.getContentType().getValue());
httpentity = new StringEntity(s);
Assert.assertEquals("text/plain; charset=ISO-8859-1",
httpentity.getContentType().getValue());
}
示例3: testNullCharset
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testNullCharset() throws Exception {
final String s = constructString(SWISS_GERMAN_HELLO);
StringEntity httpentity = new StringEntity(s, ContentType.create("text/plain", (Charset) null));
Assert.assertNotNull(httpentity.getContentType());
Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
Assert.assertEquals(s, EntityUtils.toString(httpentity));
httpentity = new StringEntity(s, (Charset) null);
Assert.assertNotNull(httpentity.getContentType());
Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
Assert.assertEquals(s, EntityUtils.toString(httpentity));
httpentity = new StringEntity(s, (String) null);
Assert.assertNotNull(httpentity.getContentType());
Assert.assertEquals("text/plain", httpentity.getContentType().getValue());
Assert.assertEquals(s, EntityUtils.toString(httpentity));
}
示例4: testBasics
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testBasics() throws Exception {
final File tmpfile = File.createTempFile("testfile", ".txt");
tmpfile.deleteOnExit();
final FileEntity httpentity = new FileEntity(tmpfile, ContentType.TEXT_PLAIN);
Assert.assertEquals(tmpfile.length(), httpentity.getContentLength());
final InputStream content = httpentity.getContent();
Assert.assertNotNull(content);
content.close();
Assert.assertTrue(httpentity.isRepeatable());
Assert.assertFalse(httpentity.isStreaming());
if (!tmpfile.delete()){
Assert.fail("Failed to delete: "+tmpfile);
}
}
示例5: testStringBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testStringBody() throws Exception {
final StringBody b1 = new StringBody("text", ContentType.DEFAULT_TEXT);
Assert.assertEquals(4, b1.getContentLength());
Assert.assertEquals("ISO-8859-1", b1.getCharset());
Assert.assertNull(b1.getFilename());
Assert.assertEquals("text/plain", b1.getMimeType());
Assert.assertEquals("text", b1.getMediaType());
Assert.assertEquals("plain", b1.getSubType());
Assert.assertEquals(MIME.ENC_8BIT, b1.getTransferEncoding());
final StringBody b2 = new StringBody("more text",
ContentType.create("text/other", MIME.DEFAULT_CHARSET));
Assert.assertEquals(9, b2.getContentLength());
Assert.assertEquals(MIME.DEFAULT_CHARSET.name(), b2.getCharset());
Assert.assertNull(b2.getFilename());
Assert.assertEquals("text/other", b2.getMimeType());
Assert.assertEquals("text", b2.getMediaType());
Assert.assertEquals("other", b2.getSubType());
Assert.assertEquals(MIME.ENC_8BIT, b2.getTransferEncoding());
}
示例6: testInputStreamBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testInputStreamBody() throws Exception {
final byte[] stuff = "Stuff".getBytes("US-ASCII");
final InputStreamBody b1 = new InputStreamBody(new ByteArrayInputStream(stuff), "stuff");
Assert.assertEquals(-1, b1.getContentLength());
Assert.assertNull(b1.getCharset());
Assert.assertEquals("stuff", b1.getFilename());
Assert.assertEquals("application/octet-stream", b1.getMimeType());
Assert.assertEquals("application", b1.getMediaType());
Assert.assertEquals("octet-stream", b1.getSubType());
Assert.assertEquals(MIME.ENC_BINARY, b1.getTransferEncoding());
final InputStreamBody b2 = new InputStreamBody(
new ByteArrayInputStream(stuff), ContentType.create("some/stuff"), "stuff");
Assert.assertEquals(-1, b2.getContentLength());
Assert.assertNull(b2.getCharset());
Assert.assertEquals("stuff", b2.getFilename());
Assert.assertEquals("some/stuff", b2.getMimeType());
Assert.assertEquals("some", b2.getMediaType());
Assert.assertEquals("stuff", b2.getSubType());
Assert.assertEquals(MIME.ENC_BINARY, b2.getTransferEncoding());
}
示例7: testParseUTF8Entity
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testParseUTF8Entity() throws Exception {
final String ru_hello = constructString(RUSSIAN_HELLO);
final String ch_hello = constructString(SWISS_GERMAN_HELLO);
final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("russian", ru_hello));
parameters.add(new BasicNameValuePair("swiss", ch_hello));
final String s = URLEncodedUtils.format(parameters, Consts.UTF_8);
Assert.assertEquals("russian=%D0%92%D1%81%D0%B5%D0%BC_%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82" +
"&swiss=Gr%C3%BCezi_z%C3%A4m%C3%A4", s);
final StringEntity entity = new StringEntity(s, ContentType.create(
URLEncodedUtils.CONTENT_TYPE, Consts.UTF_8));
final List <NameValuePair> result = URLEncodedUtils.parse(entity);
Assert.assertEquals(2, result.size());
assertNameValuePair(result.get(0), "russian", ru_hello);
assertNameValuePair(result.get(1), "swiss", ch_hello);
}
示例8: testParseEntityDefaultContentType
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testParseEntityDefaultContentType() throws Exception {
final String ch_hello = constructString(SWISS_GERMAN_HELLO);
final String us_hello = "hi there";
final List <NameValuePair> parameters = new ArrayList<NameValuePair>();
parameters.add(new BasicNameValuePair("english", us_hello));
parameters.add(new BasicNameValuePair("swiss", ch_hello));
final String s = URLEncodedUtils.format(parameters, HTTP.DEF_CONTENT_CHARSET);
Assert.assertEquals("english=hi+there&swiss=Gr%FCezi_z%E4m%E4", s);
final StringEntity entity = new StringEntity(s, ContentType.create(
URLEncodedUtils.CONTENT_TYPE, HTTP.DEF_CONTENT_CHARSET));
final List <NameValuePair> result = URLEncodedUtils.parse(entity);
Assert.assertEquals(2, result.size());
assertNameValuePair(result.get(0), "english", us_hello);
assertNameValuePair(result.get(1), "swiss", ch_hello);
}
示例9: testWriteResponseEntity
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testWriteResponseEntity() throws Exception {
final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(outstream);
conn.bind(socket);
Assert.assertEquals(0, conn.getMetrics().getRequestCount());
final HttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest("POST",
"/stuff", HttpVersion.HTTP_1_1);
request.addHeader("User-Agent", "test");
request.addHeader("Content-Length", "3");
request.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
conn.sendRequestHeader(request);
conn.sendRequestEntity(request);
conn.flush();
Assert.assertEquals(1, conn.getMetrics().getRequestCount());
final String s = new String(outstream.toByteArray(), "ASCII");
Assert.assertEquals("POST /stuff HTTP/1.1\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
}
开发者ID:reportportal,项目名称:client-java-httpclient-repacked,代码行数:24,代码来源:TestDefaultBHttpClientConnection.java
示例10: testWriteResponseEntity
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
@Test
public void testWriteResponseEntity() throws Exception {
final ByteArrayOutputStream outstream = new ByteArrayOutputStream();
Mockito.when(socket.getOutputStream()).thenReturn(outstream);
conn.bind(socket);
Assert.assertEquals(0, conn.getMetrics().getResponseCount());
final HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1, 200, "OK");
response.addHeader("User-Agent", "test");
response.addHeader("Content-Length", "3");
response.setEntity(new StringEntity("123", ContentType.TEXT_PLAIN));
conn.sendResponseHeader(response);
conn.sendResponseEntity(response);
conn.flush();
Assert.assertEquals(1, conn.getMetrics().getResponseCount());
final String s = new String(outstream.toByteArray(), "ASCII");
Assert.assertEquals("HTTP/1.1 200 OK\r\nUser-Agent: test\r\nContent-Length: 3\r\n\r\n123", s);
}
开发者ID:reportportal,项目名称:client-java-httpclient-repacked,代码行数:23,代码来源:TestDefaultBHttpServerConnection.java
示例11: executeRequest
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* Executes request command
*
* @param command
* @return
* @throws RestEndpointIOException
* @throws RestClientException
* @throws URISyntaxException
*/
@Override
public <RQ, RS> RS executeRequest(RestCommand<RQ, RS> command) throws RestEndpointIOException {
URI uri = spliceUrl(command.getUri());
HttpUriRequest rq = null;
Serializer serializer = null;
switch (command.getHttpMethod()) {
case GET:
rq = new HttpGet(uri);
break;
case POST:
serializer = getSupportedSerializer(command.getRequest());
rq = new HttpPost(uri);
((HttpPost) rq).setEntity(new InputStreamEntity(serializer.serialize(command.getRequest()), ContentType.create(
serializer.getMimeType())));
break;
case PUT:
serializer = getSupportedSerializer(command.getRequest());
rq = new HttpPut(uri);
((HttpPut) rq).setEntity(new InputStreamEntity(serializer.serialize(command.getRequest()), ContentType.create(
serializer.getMimeType())));
break;
case DELETE:
rq = new HttpDelete(uri);
break;
case PATCH:
serializer = getSupportedSerializer(command.getRequest());
rq = new HttpPatch(uri);
((HttpPatch) rq).setEntity(new InputStreamEntity(serializer.serialize(command.getRequest()), ContentType.create(
serializer.getMimeType())));
break;
default:
throw new IllegalArgumentException("Method '" + command.getHttpMethod() + "' is unsupported");
}
return executeInternal(rq, new TypeConverterCallback<RS>(serializers, command.getType()));
}
示例12: FileBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* @since 4.1
*
* @deprecated (4.3) use {@link FileBody#FileBody(File, ContentType, String)}
* or {@link MultipartEntityBuilder}
*/
@Deprecated
public FileBody(final File file,
final String filename,
final String mimeType,
final String charset) {
this(file, ContentType.create(mimeType, charset), filename);
}
示例13: InputStreamBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* @since 4.3
*/
public InputStreamBody(final InputStream in, final ContentType contentType, final String filename) {
super(contentType);
Args.notNull(in, "Input stream");
this.in = in;
this.filename = filename;
}
示例14: StringBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* @since 4.3
*/
public StringBody(final String text, final ContentType contentType) {
super(contentType);
final Charset charset = contentType.getCharset();
final String csname = charset != null ? charset.name() : Consts.ASCII.name();
try {
this.content = text.getBytes(csname);
} catch (final UnsupportedEncodingException ex) {
// Should never happen
throw new UnsupportedCharsetException(csname);
}
}
示例15: ByteArrayBody
import com.epam.reportportal.apache.http.entity.ContentType; //导入依赖的package包/类
/**
* @since 4.3
*/
public ByteArrayBody(final byte[] data, final ContentType contentType, final String filename) {
super(contentType);
Args.notNull(data, "byte[]");
this.data = data;
this.filename = filename;
}