當前位置: 首頁>>代碼示例>>Java>>正文


Java ContentType類代碼示例

本文整理匯總了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();
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:28,代碼來源:URLEncodedUtils.java

示例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());
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:17,代碼來源:TestStringEntity.java

示例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));
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:17,代碼來源:TestStringEntity.java

示例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);
    }
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:17,代碼來源:TestFileEntity.java

示例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());
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:27,代碼來源:TestMultipartContentBody.java

示例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());
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:26,代碼來源:TestMultipartContentBody.java

示例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);
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:21,代碼來源:TestURLEncodedUtils.java

示例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);
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:20,代碼來源:TestURLEncodedUtils.java

示例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()));
}
 
開發者ID:reportportal,項目名稱:client-java-rest-core,代碼行數:46,代碼來源:HttpClientRestEndpoint.java

示例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);
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:14,代碼來源:FileBody.java

示例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;
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:10,代碼來源:InputStreamBody.java

示例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);
    }
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:15,代碼來源:StringBody.java

示例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;
}
 
開發者ID:reportportal,項目名稱:client-java-httpclient-repacked,代碼行數:10,代碼來源:ByteArrayBody.java


注:本文中的com.epam.reportportal.apache.http.entity.ContentType類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。