List of usage examples for org.apache.wicket.util.resource IResourceStream getContentType
String getContentType();
From source file:org.cast.cwm.ResourceDirectory.java
License:Open Source License
/** * creates a new resource response based on the request attributes * /*w w w. jav a 2 s . co m*/ * @param attributes * current request attributes from client * @return resource response for answering request */ @Override protected ResourceResponse newResourceResponse(Attributes attributes) { String relativePath = ""; PageParameters parameters = attributes.getParameters(); for (int i = 0; i < parameters.getIndexedCount(); i++) { relativePath += parameters.get(i); relativePath += '/'; } if (relativePath.endsWith("/")) relativePath = relativePath.substring(0, relativePath.length() - 1); log.trace("relativePath is {}", relativePath); String absolutePath = new File(sourceDirectory, relativePath).getAbsolutePath(); final ResourceResponse resourceResponse = new ResourceResponse(); final IResourceStream resourceStream = getResourceStream(absolutePath); // bail out if resource stream could not be found if (resourceStream == null) { return sendResourceError(absolutePath, resourceResponse, HttpServletResponse.SC_NOT_FOUND, "Unable to find resource"); } // allow caching resourceResponse.setCacheScope(WebResponse.CacheScope.PUBLIC); resourceResponse.setCacheDuration(cacheDuration); // add Last-Modified header (to support HEAD requests and If-Modified-Since) resourceResponse.setLastModified(resourceStream.lastModifiedTime()); if (resourceResponse.dataNeedsToBeWritten(attributes)) { String contentType = resourceStream.getContentType(); if (contentType == null && Application.exists()) contentType = Application.get().getMimeType(absolutePath); // set Content-Type (may be null) resourceResponse.setContentType(contentType); try { // read resource data final byte[] bytes; bytes = IOUtils.toByteArray(resourceStream.getInputStream()); // send Content-Length header resourceResponse.setContentLength(bytes.length); // send response body with resource data resourceResponse.setWriteCallback(new WriteCallback() { @Override public void writeData(Attributes attributes) { attributes.getResponse().write(bytes); } }); } catch (IOException e) { log.debug(e.getMessage(), e); return sendResourceError(absolutePath, resourceResponse, 500, "Unable to read resource stream"); } catch (ResourceStreamNotFoundException e) { log.debug(e.getMessage(), e); return sendResourceError(absolutePath, resourceResponse, 500, "Unable to open resource stream"); } finally { try { resourceStream.close(); } catch (IOException e) { log.warn("Unable to close the resource stream", e); } } } return resourceResponse; }
From source file:org.wicketstuff.html5.image.CanvasImage.java
License:Apache License
/** * Renders the complete image tag with the base64 encoded content. */// w w w .j av a 2 s .c o m @Override protected void onComponentTag(ComponentTag tag) { checkComponentTag(tag, "img"); super.onComponentTag(tag); IResourceStream packageResourceStream = packageResourceReference.getResource().getResourceStream(); if (packageResourceStream != null) { try { StringBuilder builder = new StringBuilder(); builder.append("data:"); builder.append(packageResourceStream.getContentType()); builder.append(";base64,"); byte[] bytes = new byte[(int) packageResourceStream.length().bytes()]; DataInputStream dataInputStream = new DataInputStream(packageResourceStream.getInputStream()); dataInputStream.readFully(bytes); builder.append(Base64.encodeBase64String(bytes)); tag.put("src", builder.toString()); } catch (Exception e) { throw new WicketRuntimeException("An error occured while reading the package resource stream", e); } } else { // If the package resource stream is not set create an empty image tag.put("src", "#"); tag.put("style", "display:none;"); } }