List of usage examples for com.google.common.hash HashingOutputStream close
@Override public void close() throws IOException
From source file:uk.ac.horizon.artcodes.server.christmas.ImageServlet.java
@Override public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException { try {/* ww w . j av a2 s . c o m*/ if (request.getContentLength() > image_size) { throw new HTTPException(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Image too large"); } verifyApp(request); final String id = getImageID(request); final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); final GcsFilename filename = new GcsFilename(request.getServerName(), id); final GcsFileMetadata metadata = gcsService.getMetadata(filename); if (metadata != null) { throw new HTTPException(HttpServletResponse.SC_FORBIDDEN, "Cannot modify"); } final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream()); final String mimetype = URLConnection.guessContentTypeFromStream(inputStream); if (mimetype == null) { throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Unrecognised image type"); } final GcsFileOptions.Builder fileOptionsBuilder = new GcsFileOptions.Builder(); fileOptionsBuilder.mimeType(mimetype); final GcsFileOptions fileOptions = fileOptionsBuilder.build(); final GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions); final HashingOutputStream outputStream = new HashingOutputStream(Hashing.sha256(), Channels.newOutputStream(outputChannel)); ByteStreams.copy(inputStream, outputStream); String hash = outputStream.hash().toString(); if (!hash.equals(id)) { gcsService.delete(filename); throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Invalid hash"); } outputStream.close(); outputChannel.close(); } catch (HTTPException e) { e.writeTo(response); } }
From source file:uk.ac.horizon.artcodes.server.ImageServlet.java
@Override public void doPut(HttpServletRequest request, HttpServletResponse response) throws IOException { try {// www .j a v a2 s . c o m if (request.getContentLength() > image_size) { throw new HTTPException(HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE, "Image too large"); } verifyUser(getUser(request)); final String id = getImageID(request); final GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance()); final GcsFilename filename = new GcsFilename(request.getServerName(), id); final GcsFileMetadata metadata = gcsService.getMetadata(filename); if (metadata != null) { throw new HTTPException(HttpServletResponse.SC_FORBIDDEN, "Cannot modify"); } final BufferedInputStream inputStream = new BufferedInputStream(request.getInputStream()); final String mimetype = URLConnection.guessContentTypeFromStream(inputStream); if (mimetype == null) { throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Unrecognised image type"); } final GcsFileOptions.Builder fileOptionsBuilder = new GcsFileOptions.Builder(); fileOptionsBuilder.mimeType(mimetype); final GcsFileOptions fileOptions = fileOptionsBuilder.build(); final GcsOutputChannel outputChannel = gcsService.createOrReplace(filename, fileOptions); final HashingOutputStream outputStream = new HashingOutputStream(Hashing.sha256(), Channels.newOutputStream(outputChannel)); ByteStreams.copy(inputStream, outputStream); String hash = outputStream.hash().toString(); if (!hash.equals(id)) { gcsService.delete(filename); throw new HTTPException(HttpServletResponse.SC_BAD_REQUEST, "Invalid hash"); } outputStream.close(); outputChannel.close(); } catch (HTTPException e) { e.writeTo(response); } }