/** * Returns the mime-type associated with this extension, or the server's default. */ public static String getMIMETypeOrDefault(String extension) { String mimeType = getMIMEType(extension); if (StringUtils.isNotEmpty(mimeType)) { return mimeType; } if (StringUtils.isNotEmpty(extension)) { log.info("Cannot find MIME type for extension \"{}\"", extension); } String defaultExtension = ServerConfiguration.getInstance().getDefaultExtension(); if (StringUtils.isBlank(defaultExtension)) { defaultExtension = DEFAULT_EXTENSION; } return getMIMEType(defaultExtension); }
protected String getMimeType(String extension, HttpServletResponse response) { final String mimeType; if (isRegisteredExtensionsOnly()) { if (StringUtils.isBlank(extension)) { extension = serverConfiguration.getDefaultExtension(); if (StringUtils.isBlank(extension)) { extension = MIMEMapping.DEFAULT_EXTENSION; } } mimeType = MIMEMapping.getMIMEType(extension); if (mimeType == null) { return null; } } else { mimeType = MIMEMapping.getMIMETypeOrDefault(extension); } return mimeType; }
public String getContentType() { return MIMEMapping.getMIMEType(StringUtils.substringAfterLast(this.getPath(), ".")); }
@Override protected boolean boolVote(Object value) { String extension; if (value instanceof String) { extension = StringUtils.substringAfterLast((String) value, "."); } else { extension = MgnlContext.getAggregationState().getExtension(); } if (StringUtils.isEmpty(MIMEMapping.getMIMEType(extension))) { return false; // check for MIMEMapping, extension must exist } if (allow != null && allow.length > 0 && !ArrayUtils.contains(allow, extension)) { return false; } if (deny != null && deny.length > 0 && ArrayUtils.contains(deny, extension)) { return false; } return true; }
protected void serveResource(HttpServletResponse response, Resource resource) throws IOException { final String extension = StringUtils.substringAfterLast(resource.getName(), "."); final String mimeType = MIMEMapping.getMIMEType(extension); response.setContentType(mimeType); response.setDateHeader(HttpHeaders.LAST_MODIFIED, resource.getLastModified()); try (InputStream in = resource.openStream(); OutputStream out = response.getOutputStream()) { IOUtils.copy(in, out); out.flush(); } catch (IOException e) { log.debug("Can't load resource {} : {}", resource, e, e); response.sendError(HttpServletResponse.SC_NOT_FOUND); // tomcat usually throws a ClientAbortException anytime the user stop loading the page log.debug("Unable to serve resource due to a {} exception: ", e, e); if (!response.isCommitted()) { response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } } } }
@Override public void setContentType(String type) { super.setContentType(type); if (!StringUtils.isEmpty(extension) && !isCommitted()) { final String contentType = MIMEMapping.getMIMEType(extension); if (!StringUtils.contains(type, contentType)) { try { super.sendError(HttpServletResponse.SC_BAD_REQUEST, String.format("Invalid Content-Type for given extension=%1$s.", type)); } catch (IOException e) { log.error("Couldn't send error on the response. ", e); } return; } } } }
private String getMimeType(Node node) throws RepositoryException { if (isAssetNode(node)) { String mimeType = PropertyUtil.getString(node.getNode(RESOURCE_NAME), MIMETYPE, ""); if (StringUtils.isBlank(mimeType)) { String extension = PropertyUtil.getString(node.getNode(RESOURCE_NAME), EXTENSION, ""); return MIMEMapping.getMIMEType(extension); } return mimeType; } return null; }
/** * The template is stored in the content node. So we don't need a * templatePath. */ @Override protected String resolveTemplateScript(Node content, RenderableDefinition definition, RenderingModel<?> model, final String actionResult) { if (shouldBypass(content)) { try { String path; if (StringUtils.isNotBlank(MIMEMapping.getMIMEType(StringUtils.substringAfterLast(content.getPath(), ".")))) { return content.getPath(); } String extension = PropertyUtil.getString(content, "extension"); path = content.getPath(); if (extension != null) { path += "." + extension; } return path; } catch (RepositoryException re) { log.error("Not able to determineTemplatePath ", re); } } // Return an empty object due to the check of AbstractRenderer.render(. // that throw an Exception if the templatePath is null. In our case // we don't have a templatePath because the script is coming from // the content node. return ""; }
@Override public void render(RenderingContext ctx, Map<String, Object> contextObjects) throws RenderException { Node content = ctx.getCurrentContent(); InputStream stream = null; try { stream = this.getInputStream(content); final HttpServletResponse response = MgnlContext.getWebContext().getResponse(); response.setContentLength(stream.available()); if (content.hasNode(BINARY_NODE) && content.getNode(BINARY_NODE).hasProperty(JcrConstants.JCR_MIMETYPE)) { final String mimeType = content.getNode(BINARY_NODE).getProperty(JcrConstants.JCR_MIMETYPE).getString(); response.setContentType(mimeType); } else if (content.hasProperty(EXTENSION_PROPERTY)) { final String extension = content.getProperty(EXTENSION_PROPERTY).getString(); final String mimeType = MIMEMapping.getMIMEType(extension); response.setContentType(mimeType); } IOUtils.copy(stream, ctx.getOutputStream()); } catch (RepositoryException e) { throw new RenderException("Can't read binary data for resource " + content, e); } catch (IOException e) { throw new RenderException("Can't write binary data to the output stream.", e); } finally { IOUtils.closeQuietly(stream); } }
nd.setAttribute(FileProperties.PROPERTY_FILENAME, label); nd.setAttribute(FileProperties.PROPERTY_SIZE, "" + bytes.length); String mimetype = StringUtils.defaultIfEmpty(MIMEMapping.getMIMEType(extension), "application/octet-stream"); nd.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, mimetype);