Search in sources :

Example 31 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project async-http-client by AsyncHttpClient.

the class HttpStaticFileServerHandler method setContentTypeHeader.

/**
 * Sets the content type header for the HTTP Response
 *
 * @param response HTTP response
 * @param file     file to extract content type
 */
private static void setContentTypeHeader(HttpResponse response, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    response.headers().set(CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap)

Example 32 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project indy by Commonjava.

the class SetContentTypeNotExistsTest method run.

@Test
public void run() throws Exception {
    final String content = "This is some content " + System.currentTimeMillis() + "." + System.nanoTime();
    final String path = "org/foo/foo-project/1/foo-1.nodefine";
    server.expect("GET", server.formatUrl(STORE, path), (request, response) -> {
        response.setStatus(200);
        response.setHeader("Content-Length", Integer.toString(content.length()));
        PrintWriter writer = response.getWriter();
        writer.write(content);
    });
    client.stores().create(new RemoteRepository(STORE, server.formatUrl(STORE)), "adding remote", RemoteRepository.class);
    try (HttpResources httpResources = client.module(IndyRawHttpModule.class).getHttp().getRaw(client.content().contentPath(remote, STORE, path))) {
        HttpResponse response = httpResources.getResponse();
        String contentType = response.getFirstHeader("Content-Type").getValue();
        assertThat(contentType, equalTo(new MimetypesFileTypeMap().getContentType(path)));
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) HttpResources(org.commonjava.indy.client.core.helper.HttpResources) HttpResponse(org.apache.http.HttpResponse) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 33 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project spring-framework by spring-projects.

the class MockServletContextTests method getMimeTypeWithCustomConfiguredType.

/**
	 * Introduced to dispel claims in a thread on Stack Overflow:
	 * <a href="http://stackoverflow.com/questions/22986109/testing-spring-managed-servlet">Testing Spring managed servlet</a>
	 */
@Test
public void getMimeTypeWithCustomConfiguredType() {
    FileTypeMap defaultFileTypeMap = FileTypeMap.getDefaultFileTypeMap();
    assertThat(defaultFileTypeMap, instanceOf(MimetypesFileTypeMap.class));
    MimetypesFileTypeMap mimetypesFileTypeMap = (MimetypesFileTypeMap) defaultFileTypeMap;
    mimetypesFileTypeMap.addMimeTypes("text/enigma    enigma");
    assertEquals("text/enigma", sc.getMimeType("filename.enigma"));
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) FileTypeMap(javax.activation.FileTypeMap) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Test(org.junit.Test)

Example 34 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project camel by apache.

the class SolrProducer method insert.

private void insert(Exchange exchange, SolrClient solrServer) throws Exception {
    Object body = exchange.getIn().getBody();
    boolean invalid = false;
    if (body instanceof WrappedFile) {
        body = ((WrappedFile<?>) body).getFile();
    }
    if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class))) {
        String mimeType = exchange.getIn().getHeader(Exchange.CONTENT_TYPE, String.class);
        ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
        updateRequest.addFile((File) body, mimeType);
        for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
            if (entry.getKey().startsWith(SolrConstants.PARAM)) {
                String paramName = entry.getKey().substring(SolrConstants.PARAM.length());
                updateRequest.setParam(paramName, entry.getValue().toString());
            }
        }
        updateRequest.process(solrServer);
    } else {
        if (body instanceof File) {
            MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
            String mimeType = mimeTypesMap.getContentType((File) body);
            ContentStreamUpdateRequest updateRequest = new ContentStreamUpdateRequest(getRequestHandler());
            updateRequest.addFile((File) body, mimeType);
            for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
                if (entry.getKey().startsWith(SolrConstants.PARAM)) {
                    String paramName = entry.getKey().substring(SolrConstants.PARAM.length());
                    updateRequest.setParam(paramName, entry.getValue().toString());
                }
            }
            updateRequest.process(solrServer);
        } else if (body instanceof SolrInputDocument) {
            UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
            updateRequest.add((SolrInputDocument) body);
            updateRequest.process(solrServer);
        } else if (body instanceof List<?>) {
            List<?> list = (List<?>) body;
            if (list.size() > 0 && list.get(0) instanceof SolrInputDocument) {
                UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
                updateRequest.add((List<SolrInputDocument>) list);
                updateRequest.process(solrServer);
            } else {
                invalid = true;
            }
        } else {
            boolean hasSolrHeaders = false;
            for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
                if (entry.getKey().startsWith(SolrConstants.FIELD)) {
                    hasSolrHeaders = true;
                    break;
                }
            }
            if (hasSolrHeaders) {
                UpdateRequest updateRequest = new UpdateRequest(getRequestHandler());
                SolrInputDocument doc = new SolrInputDocument();
                for (Map.Entry<String, Object> entry : exchange.getIn().getHeaders().entrySet()) {
                    if (entry.getKey().startsWith(SolrConstants.FIELD)) {
                        String fieldName = entry.getKey().substring(SolrConstants.FIELD.length());
                        doc.setField(fieldName, entry.getValue());
                    }
                }
                updateRequest.add(doc);
                updateRequest.process(solrServer);
            } else if (body instanceof String) {
                String bodyAsString = (String) body;
                if (!bodyAsString.startsWith("<add")) {
                    bodyAsString = "<add>" + bodyAsString + "</add>";
                }
                DirectXmlRequest xmlRequest = new DirectXmlRequest(getRequestHandler(), bodyAsString);
                solrServer.request(xmlRequest);
            } else {
                invalid = true;
            }
        }
    }
    if (invalid) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unable to find data in Exchange to update Solr");
    }
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) ContentStreamUpdateRequest(org.apache.solr.client.solrj.request.ContentStreamUpdateRequest) DirectXmlRequest(org.apache.solr.client.solrj.request.DirectXmlRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) WrappedFile(org.apache.camel.WrappedFile) List(java.util.List) MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap) Map(java.util.Map) File(java.io.File) WrappedFile(org.apache.camel.WrappedFile) SolrException(org.apache.solr.common.SolrException)

Example 35 with MimetypesFileTypeMap

use of javax.activation.MimetypesFileTypeMap in project netty by netty.

the class Http2StaticFileServerHandler method setContentTypeHeader.

/**
 * Sets the content type header for the HTTP Response
 *
 * @param headers Http2 Headers
 * @param file    file to extract content type
 */
private static void setContentTypeHeader(Http2Headers headers, File file) {
    MimetypesFileTypeMap mimeTypesMap = new MimetypesFileTypeMap();
    headers.set(HttpHeaderNames.CONTENT_TYPE, mimeTypesMap.getContentType(file.getPath()));
}
Also used : MimetypesFileTypeMap(javax.activation.MimetypesFileTypeMap)

Aggregations

MimetypesFileTypeMap (javax.activation.MimetypesFileTypeMap)63 File (java.io.File)19 IOException (java.io.IOException)15 FileInputStream (java.io.FileInputStream)7 InputStream (java.io.InputStream)7 DefaultStreamedContent (org.primefaces.model.DefaultStreamedContent)6 URL (java.net.URL)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 FileNameMap (java.net.FileNameMap)3 HttpURLConnection (java.net.HttpURLConnection)3 MimeType (javax.activation.MimeType)3 MimeTypeParseException (javax.activation.MimeTypeParseException)3 Response (javax.ws.rs.core.Response)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Failure (org.junit.runner.notification.Failure)3 InvalidKeyException (java.security.InvalidKeyException)2