/** * Returns the content-type from a file name. If none is found returning * application/octet-stream<br/> * For a text-based content-type, also return the encoding suffix eg. * <em>"text/plain; charset=utf-8"</em> * * @param filename the file name * @return the content-type deduced from the file extension. */ public String getContentType(String filename) { return getContentType(filename, "application/octet-stream"); }
/** * Returns the mimetype from a file name * * @param filename the file name * @return the mimetype or the empty string if not found */ public String getMimeType(String filename) { return getMimeType(filename, ""); }
public MimeTypes(PippoSettings pippoSettings) { this.pippoSettings = pippoSettings; this.extPattern = Pattern.compile("^.*\\.([^.]+)$"); mimetypes = new Properties(); init(); }
public Application(PippoSettings settings) { this.pippoSettings = settings; this.languages = new Languages(settings); this.messages = new Messages(languages); this.mimeTypes = new MimeTypes(settings); this.httpCacheToolkit = new HttpCacheToolkit(settings); this.engines = new ContentTypeEngines(); this.initializers = new ArrayList<>(); this.webSocketRouter = new WebSocketRouter(); registerContentTypeEngine(TextPlainEngine.class); }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().contentType(mimeType); routeContext.getResponse().ok().resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().file(filename, resourceUrl.openStream()); } }
/** * Returns the content-type from a file name.<br/> * For a text-based content-type, also return the encoding suffix eg. * <em>"text/plain; charset=utf-8"</em> * * @param filename the file name * @param defaultContentType the default content-type to return when no matching * content-type is found * @return the content-type deduced from the file extension. */ public String getContentType(String filename, String defaultContentType) { String contentType = getMimeType(filename, null); if (contentType == null) { contentType = defaultContentType; } if (contentType != null && contentType.startsWith("text/")) { // UTF-8 is fixed for now as Pippo only supports utf-8 in files... return contentType + "; charset=utf-8"; } return contentType; }
protected void sendResource(URL resourceUrl, RouteContext routeContext) throws IOException { String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { // stream the resource log.debug("Streaming as resource '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).resource(resourceUrl.openStream()); } else { // stream the file log.debug("Streaming as file '{}'", resourceUrl); routeContext.getResponse().ok().chunked(chunked).file(filename, resourceUrl.openStream()); } }
protected void setResponseHeaders(URL resourceUrl, RouteContext routeContext) { try { long lastModified = resourceUrl.openConnection().getLastModified(); routeContext.getApplication().getHttpCacheToolkit().addEtag(routeContext, lastModified); String filename = resourceUrl.getFile(); String mimeType = routeContext.getApplication().getMimeTypes().getContentType(filename); if (!StringUtils.isNullOrEmpty(mimeType)) { routeContext.getResponse().contentType(mimeType); } } catch (Exception e) { throw new PippoRuntimeException(e, "Failed to stream resource {}", resourceUrl); } }
/** * Copies the input stream to the response output stream as a download and closes the input stream upon completion. * <p>This method commits the response.</p> * * @param filename * @param input */ public void file(String filename, InputStream input) { checkCommitted(); // content type to OCTET_STREAM if it's not set if (getContentType() == null) { contentType(mimeTypes.getContentType(filename, HttpConstants.ContentType.APPLICATION_OCTET_STREAM)); } if (isHeaderEmpty(HttpConstants.Header.CONTENT_DISPOSITION)) { filenameHeader(filename); } finalizeResponse(); try { send(input); } catch (IOException e) { throw new PippoRuntimeException(e); } }