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()));
}
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)));
}
}
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"));
}
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");
}
}
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()));
}
Aggregations