protected void doGet()

in src/main/java/org/apache/sling/reseditor/DownloadBinaryProperty.java [69:123]


	protected void doGet(SlingHttpServletRequest request,
			SlingHttpServletResponse response) throws ServletException,
			IOException {
		String propertyName = request.getParameter("property");
		if ("".equals(propertyName) || propertyName == null)
			throw new ResourceNotFoundException("No property specified.");

		ServletOutputStream out = response.getOutputStream();
		Resource resource = request.getResource();
		Node currentNode = resource.adaptTo(Node.class);
		javax.jcr.Property property = null;
		try {
			property = currentNode.getProperty(propertyName);
		} catch (PathNotFoundException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
			throw new ResourceNotFoundException("Not found.");
		} catch (RepositoryException e) {
			response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
			throw new ResourceNotFoundException("Not found.");
		}
		InputStream stream = null;
		try {
			if (property == null || property.getType() != PropertyType.BINARY) {
				response.sendError(HttpServletResponse.SC_NOT_FOUND, "Not found.");
				throw new ResourceNotFoundException("Not found.");
			}
			long length = property.getLength();
			if (length > 0) {
				if (length < Integer.MAX_VALUE) {
					response.setContentLength((int) length);
				} else {
					response.setHeader("Content-Length", String.valueOf(length));
				}
			}
			stream = property.getStream();
			byte[] buf = new byte[2048];
			int rd;
			while ((rd = stream.read(buf)) >= 0) {
				out.write(buf, 0, rd);
			}
		} catch (ValueFormatException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Error downloading the property content.");
			log.debug("error reading the property " + property + " of path "
					+ resource.getPath(), e);
		} catch (RepositoryException e) {
			response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
					"Error downloading the property content.");
			log.debug("error reading the property " + property + " of path "
					+ resource.getPath(), e);
		} finally {
			stream.close();
		}

	}