Search in sources :

Example 21 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class ContentTypeTest method parse_with_parameters.

@Test
public void parse_with_parameters() throws Exception {
    ContentType contentType = new ContentType("text/html;charset=utf-8");
    assertEquals(contentType.getBaseType(), "text");
    assertEquals(contentType.getSubType(), "html");
    assertEquals(contentType.getMimeType(), "text/html");
    List<String> parameterNames = contentType.getParameterNames();
    assertEquals(parameterNames.size(), 1);
    assertEquals(parameterNames.get(0), "charset");
    assertEquals(contentType.getCharset(), "utf-8");
    assertTrue(contentType.hasParameters());
    String nonexistant = contentType.getParameter("nonexistant");
    assertTrue(nonexistant == null);
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) Test(org.testng.annotations.Test)

Example 22 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class ContentTypeTest method simple_equals.

@Test
public void simple_equals() {
    ContentType master = new ContentType("text/html");
    assertFalse(master.equals(null));
    assertFalse(master.equals(this));
    assertTrue(master.equals(master));
    assertTrue(master.equals(new ContentType("text/html")));
    assertFalse(master.equals(new ContentType("foo/bar")));
    assertFalse(master.equals(new ContentType("text/plain")));
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) Test(org.testng.annotations.Test)

Example 23 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class ContentTypeTest method parse_without_parameters.

@Test
public void parse_without_parameters() throws Exception {
    ContentType contentType = new ContentType("text/html");
    assertEquals(contentType.getBaseType(), "text");
    assertEquals(contentType.getSubType(), "html");
    assertEquals(contentType.getMimeType(), "text/html");
    assertTrue(contentType.getParameterNames().isEmpty());
    assertFalse(contentType.hasParameters());
}
Also used : ContentType(org.apache.tapestry5.http.ContentType) Test(org.testng.annotations.Test)

Example 24 with ContentType

use of org.apache.tapestry5.http.ContentType in project tapestry-5 by apache.

the class TapestryModule method contributeMetaWorker.

/**
 * Contributes extractors for {@link Meta}, {@link Secure}, {@link ContentType} and {@link WhitelistAccessOnly} annotations.
 *
 * @since 5.2.0
 */
@SuppressWarnings("unchecked")
public static void contributeMetaWorker(MappedConfiguration<Class, MetaDataExtractor> configuration) {
    configuration.addInstance(Meta.class, MetaAnnotationExtractor.class);
    configuration.add(Secure.class, new FixedExtractor(MetaDataConstants.SECURE_PAGE));
    configuration.addInstance(ContentType.class, ContentTypeExtractor.class);
    configuration.add(WhitelistAccessOnly.class, new FixedExtractor(MetaDataConstants.WHITELIST_ONLY_PAGE));
    configuration.addInstance(UnknownActivationContextCheck.class, UnknownActivationContextExtractor.class);
}
Also used : FixedExtractor(org.apache.tapestry5.services.meta.FixedExtractor)

Example 25 with ContentType

use of org.apache.tapestry5.http.ContentType in project httpcomponents-client by apache.

the class AsyncEchoHandler method handleRequest.

@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
    final String method = request.getMethod();
    if (!"GET".equalsIgnoreCase(method) && !"HEAD".equalsIgnoreCase(method) && !"POST".equalsIgnoreCase(method) && !"PUT".equalsIgnoreCase(method)) {
        throw new MethodNotSupportedException(method + " not supported by " + getClass().getName());
    }
    if (entityDetails != null) {
        final ContentType contentType = ContentType.parseLenient(entityDetails.getContentType());
        entityConsumer.streamStart(entityDetails, new FutureCallback<byte[]>() {

            @Override
            public void completed(final byte[] content) {
                final BasicAsyncEntityProducer entityProducer = new BasicAsyncEntityProducer(content, contentType);
                entityProducerRef.set(entityProducer);
                try {
                    responseChannel.sendResponse(new BasicHttpResponse(HttpStatus.SC_OK), entityProducer, context);
                } catch (final IOException | HttpException ex) {
                    failed(ex);
                }
            }

            @Override
            public void failed(final Exception ex) {
                releaseResources();
            }

            @Override
            public void cancelled() {
                releaseResources();
            }
        });
    } else {
        responseChannel.sendResponse(new BasicHttpResponse(HttpStatus.SC_OK), null, context);
        entityConsumer.releaseResources();
    }
}
Also used : BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) ContentType(org.apache.hc.core5.http.ContentType) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) BasicAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.BasicAsyncEntityProducer) HttpException(org.apache.hc.core5.http.HttpException) MethodNotSupportedException(org.apache.hc.core5.http.MethodNotSupportedException) IOException(java.io.IOException)

Aggregations

ContentType (org.apache.hc.core5.http.ContentType)58 Charset (java.nio.charset.Charset)18 IOException (java.io.IOException)15 ContentType (org.apache.tapestry5.http.ContentType)15 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)13 HttpException (org.apache.hc.core5.http.HttpException)12 HttpResponse (org.apache.hc.core5.http.HttpResponse)12 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)10 HttpRequest (org.apache.hc.core5.http.HttpRequest)10 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)10 InputStream (java.io.InputStream)9 UnsupportedEncodingException (java.io.UnsupportedEncodingException)8 InetSocketAddress (java.net.InetSocketAddress)8 Header (org.apache.hc.core5.http.Header)8 Message (org.apache.hc.core5.http.Message)8 Test (org.junit.Test)8 Test (org.testng.annotations.Test)8 InterruptedIOException (java.io.InterruptedIOException)7 HttpEntity (org.apache.hc.core5.http.HttpEntity)7 StringTokenizer (java.util.StringTokenizer)6