List of usage examples for java.security DigestOutputStream flush
@Override public void flush() throws IOException
From source file:org.apache.hadoop.hdfs.qjournal.server.TestJournalNodeImageManifest.java
/** * Generate random contents for the image and store it together with the md5 * for later comparison.// w ww. j a v a2s. co m */ private ContentBody genContent(long txid) throws IOException { MessageDigest digester = MD5Hash.getDigester(); // write through digester so we can roll the written image ByteArrayOutputStream bos = new ByteArrayOutputStream(1024); DigestOutputStream ds = new DigestOutputStream(bos, digester); // generate random bytes new Random().nextBytes(randomBytes); ds.write(randomBytes); ds.flush(); // get written hash MD5Hash hash = new MD5Hash(digester.digest()); // store contents and digest digests.put(txid, hash); content.put(txid, Arrays.copyOf(randomBytes, randomBytes.length)); return new ByteArrayBody(bos.toByteArray(), "filename"); }
From source file:com.threadswarm.imagefeedarchiver.processor.RssItemProcessor.java
private void downloadRssMediaContent(ProcessedRssItem processedItem, RssMediaContent mediaContent) { DownloadStatus downloadStatus = DownloadStatus.FAILED; HttpEntity responseEntity = null;/*from ww w . ja v a2 s .c o m*/ try { String targetUrlString = mediaContent.getUrlString(); if (forceHttps) targetUrlString = FeedUtils.rewriteUrlStringToHttps(targetUrlString); URI targetURI = FeedUtils.getUriFromUrlString(targetUrlString); boolean freshURI = processedURISet.add(targetURI); if (!freshURI) { LOGGER.warn("Skipping previously processed URI: {}", targetURI); return; //abort processing } LOGGER.info("Attempting to download {}", targetURI); HttpGet imageGet = new HttpGet(targetURI); for (Header header : headerList) imageGet.addHeader(header); HttpResponse imageResponse = httpClient.execute(imageGet); String originalFileName = StringUtils.stripStart(targetURI.toURL().getFile(), "/"); originalFileName = StringUtils.replace(originalFileName, "/", "_"); File outputFile = getOutputFile(originalFileName); long expectedContentLength = FeedUtils.calculateBestExpectedContentLength(imageResponse, mediaContent); responseEntity = imageResponse.getEntity(); BufferedInputStream bis = null; DigestOutputStream fos = null; int bytesRead = 0; try { bis = new BufferedInputStream(responseEntity.getContent()); fos = new DigestOutputStream(new FileOutputStream(outputFile), MessageDigest.getInstance("SHA")); byte[] buffer = new byte[8192]; while ((bytesRead = bis.read(buffer, 0, buffer.length)) != -1) { fos.write(buffer, 0, bytesRead); } fos.flush(); MessageDigest messageDigest = fos.getMessageDigest(); byte[] digestBytes = messageDigest.digest(); String digestString = Hex.encodeHexString(digestBytes); LOGGER.info("Downloaded - {} (SHA: {})", targetURI, digestString); processedItem.setDownloadDate(new Date()); downloadStatus = DownloadStatus.COMPLETED; processedItem.setHash(digestString); processedItem.setFilename(outputFile.toString()); } catch (ConnectionClosedException e) { LOGGER.error("An Exception was thrown while attempting to read HTTP entity content", e); } catch (NoSuchAlgorithmException e) { LOGGER.error("The SHA-1 hashing algorithm is not available on this JVM", e); } finally { IOUtils.closeQuietly(bis); IOUtils.closeQuietly(fos); EntityUtils.consumeQuietly(responseEntity); if (downloadStatus == DownloadStatus.FAILED || (outputFile.exists() && outputFile.length() != expectedContentLength)) { LOGGER.warn("Deleted partial/failed file: {}", outputFile); outputFile.delete(); processedItem.setDownloadStatus(DownloadStatus.FAILED); } } } catch (IOException e) { LOGGER.error("An Exception was thrown while attempting to download image content", e); } catch (URISyntaxException e) { LOGGER.error("The supplied URI, {}, violates syntax rules", e); } finally { EntityUtils.consumeQuietly(responseEntity); } processedItem.setDownloadStatus(downloadStatus); itemDAO.save(processedItem); }
From source file:com.jpeterson.littles3.StorageEngine.java
/** * Write/* w ww .j a va2 s.co m*/ * * @param req * the HttpServletRequest object that contains the request the * client made of the servlet * @param resp * the HttpServletResponse object that contains the response the * servlet returns to the client * @throws IOException * if an input or output error occurs while the servlet is * handling the PUT request * @throws ServletException * if the request for the PUT cannot be handled */ @SuppressWarnings("unchecked") public void methodPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { OutputStream out = null; try { S3ObjectRequest or; try { or = S3ObjectRequest.create(req, resolvedHost(), (Authenticator) getWebApplicationContext().getBean(BEAN_AUTHENTICATOR)); } catch (InvalidAccessKeyIdException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_FORBIDDEN, "InvalidAccessKeyId"); return; } catch (InvalidSecurityException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_FORBIDDEN, "InvalidSecurity"); return; } catch (RequestTimeTooSkewedException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_FORBIDDEN, "RequestTimeTooSkewed"); return; } catch (SignatureDoesNotMatchException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_FORBIDDEN, "SignatureDoesNotMatch"); return; } catch (AuthenticatorException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_FORBIDDEN, "InvalidSecurity"); return; } logger.debug("S3ObjectRequest: " + or); CanonicalUser requestor = or.getRequestor(); if (or.getKey() != null) { String value; long contentLength; MessageDigest messageDigest = MessageDigest.getInstance("MD5"); DigestOutputStream digestOutputStream = null; S3Object oldS3Object = null; S3Object s3Object; StorageService storageService; Bucket bucket; String bucketName = or.getBucket(); String key = or.getKey(); if (!isValidKey(key)) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "KeyTooLong"); return; } storageService = (StorageService) getWebApplicationContext().getBean(BEAN_STORAGE_SERVICE); if (req.getParameter(PARAMETER_ACL) != null) { // write access control policy Acp acp; CanonicalUser owner; s3Object = storageService.load(bucketName, key); if (s3Object == null) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, "NoSuchKey"); return; } acp = s3Object.getAcp(); try { acp.canWrite(requestor); } catch (AccessControlException e) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "AccessDenied"); return; } // save owner owner = acp.getOwner(); try { acp = Acp.decode(req.getInputStream()); } catch (IOException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "MalformedACLError"); return; } // maintain owner acp.setOwner(owner); s3Object.setAcp(acp); storageService.store(s3Object); } else { // make sure requestor can "WRITE" to the bucket try { bucket = storageService.loadBucket(bucketName); bucket.canWrite(requestor); } catch (AccessControlException e) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "AccessDenied"); return; } catch (DataAccessException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, "NoSuchBucket"); return; } try { oldS3Object = storageService.load(bucket.getName(), key); } catch (DataRetrievalFailureException e) { // ignore } // create a new S3Object for this request to store an object try { s3Object = storageService.createS3Object(bucket, key, requestor); } catch (DataAccessException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, "NoSuchBucket"); return; } out = s3Object.getOutputStream(); digestOutputStream = new DigestOutputStream(out, messageDigest); // Used instead of req.getContentLength(); because Amazon // limit is 5 gig, which is bigger than an int value = req.getHeader("Content-Length"); if (value == null) { resp.sendError(HttpServletResponse.SC_LENGTH_REQUIRED, "MissingContentLength"); return; } contentLength = Long.valueOf(value).longValue(); if (contentLength > 5368709120L) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "EntityTooLarge"); return; } long written = 0; int count; byte[] b = new byte[4096]; ServletInputStream in = req.getInputStream(); while (((count = in.read(b, 0, b.length)) > 0) && (written < contentLength)) { digestOutputStream.write(b, 0, count); written += count; } digestOutputStream.flush(); if (written != contentLength) { // transmission truncated if (out != null) { out.close(); out = null; } if (digestOutputStream != null) { digestOutputStream.close(); digestOutputStream = null; } // clean up storageService.remove(s3Object); resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "IncompleteBody"); return; } s3Object.setContentDisposition(req.getHeader("Content-Disposition")); s3Object.setContentLength(contentLength); s3Object.setContentMD5(req.getHeader("Content-MD5")); value = req.getContentType(); logger.debug("Put - Content-Type: " + value); if (value == null) { value = S3Object.DEFAULT_CONTENT_TYPE; } s3Object.setContentType(value); logger.debug("Put - get content-type: " + s3Object.getContentType()); s3Object.setLastModified(System.currentTimeMillis()); // metadata int prefixLength = HEADER_PREFIX_USER_META.length(); String name; for (Enumeration headerNames = req.getHeaderNames(); headerNames.hasMoreElements();) { String headerName = (String) headerNames.nextElement(); if (headerName.startsWith(HEADER_PREFIX_USER_META)) { name = headerName.substring(prefixLength).toLowerCase(); for (Enumeration headers = req.getHeaders(headerName); headers.hasMoreElements();) { value = (String) headers.nextElement(); s3Object.addMetadata(name, value); } } } // calculate ETag, hex encoding of MD5 value = new String(Hex.encodeHex(digestOutputStream.getMessageDigest().digest())); resp.setHeader("ETag", value); s3Object.setETag(value); grantCannedAccessPolicies(req, s3Object.getAcp(), requestor); // NOTE: This could be reengineered to have a two-phase // commit. if (oldS3Object != null) { storageService.remove(oldS3Object); } storageService.store(s3Object); } } else if (or.getBucket() != null) { StorageService storageService; Bucket bucket; storageService = (StorageService) getWebApplicationContext().getBean(BEAN_STORAGE_SERVICE); if (req.getParameter(PARAMETER_ACL) != null) { // write access control policy Acp acp; CanonicalUser owner; logger.debug("User is providing new ACP for bucket " + or.getBucket()); try { bucket = storageService.loadBucket(or.getBucket()); } catch (DataAccessException e) { resp.sendError(HttpServletResponse.SC_NOT_FOUND, "NoSuchBucket"); return; } acp = bucket.getAcp(); try { acp.canWrite(requestor); } catch (AccessControlException e) { resp.sendError(HttpServletResponse.SC_FORBIDDEN, "AccessDenied"); return; } // save owner owner = acp.getOwner(); try { acp = Acp.decode(req.getInputStream()); } catch (IOException e) { e.printStackTrace(); resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "MalformedACLError"); return; } // maintain owner acp.setOwner(owner); bucket.setAcp(acp); logger.debug("Saving bucket ACP"); logger.debug("ACP: " + Acp.encode(bucket.getAcp())); storageService.storeBucket(bucket); } else { // validate bucket String bucketName = or.getBucket(); if (!isValidBucketName(bucketName)) { resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "InvalidBucketName"); return; } try { bucket = storageService.createBucket(bucketName, requestor); } catch (BucketAlreadyExistsException e) { resp.sendError(HttpServletResponse.SC_CONFLICT, "BucketAlreadyExists"); return; } grantCannedAccessPolicies(req, bucket.getAcp(), requestor); storageService.storeBucket(bucket); } } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); logger.error("Unable to use MD5", e); resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "InternalError"); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (out != null) { out.close(); out = null; } } }
From source file:org.apache.sling.distribution.packaging.impl.FileDistributionPackageBuilder.java
@Override protected DistributionPackage readPackageInternal(@Nonnull ResourceResolver resourceResolver, @Nonnull InputStream stream) throws DistributionException { DistributionPackage distributionPackage; final File file; DigestOutputStream outputStream = null; try {// w w w .ja va 2s .co m String name; // stable id Map<String, Object> info = new HashMap<String, Object>(); DistributionPackageUtils.readInfo(stream, info); Object remoteId = info.get(DistributionPackageUtils.PROPERTY_REMOTE_PACKAGE_ID); if (remoteId != null) { name = remoteId.toString(); log.debug("preserving remote id {}", name); } else { name = "distrpck-read-" + System.nanoTime(); log.debug("generating a new id {}", name); } file = File.createTempFile(name, "." + getType(), tempDirectory); outputStream = openDigestOutputStream(new FileOutputStream(file), digestAlgorithm); IOUtils.copy(stream, outputStream); outputStream.flush(); String digestMessage = readDigestMessage(outputStream); distributionPackage = new FileDistributionPackage(file, getType(), digestAlgorithm, digestMessage); } catch (Exception e) { throw new DistributionException(e); } finally { IOUtils.closeQuietly(outputStream); } return distributionPackage; }
From source file:org.geogit.storage.AbstractObjectDatabase.java
/** * @see org.geogit.storage.ObjectDatabase#put(org.geogit.storage.ObjectWriter) *//*from w w w. ja v a2 s . c o m*/ @Override public final <T> ObjectId put(final ObjectWriter<T> writer) throws Exception { MessageDigest sha1; sha1 = MessageDigest.getInstance("SHA1"); ByteArrayOutputStream rawOut = new ByteArrayOutputStream(); DigestOutputStream keyGenOut = new DigestOutputStream(rawOut, sha1); // GZIPOutputStream cOut = new GZIPOutputStream(keyGenOut); LZFOutputStream cOut = new LZFOutputStream(keyGenOut); try { writer.write(cOut); } finally { // cOut.finish(); cOut.flush(); cOut.close(); keyGenOut.flush(); keyGenOut.close(); rawOut.flush(); rawOut.close(); } final byte[] rawData = rawOut.toByteArray(); final byte[] rawKey = keyGenOut.getMessageDigest().digest(); final ObjectId id = new ObjectId(rawKey); putInternal(id, rawData, false); return id; }
From source file:org.sourcepit.m2p2.cache.FileCacheTransport.java
private IStatus cache(URI toDownload, OutputStream target, IProgressMonitor monitor, final IArtifactDescriptor descriptor, File artifactFile, String md5) { log.log(LogService.LOG_INFO, "Downloading " + descriptor.getArtifactKey().toExternalForm()); final MessageDigest md5Digest = newMd5Digest(); final File tmpFile; try {/*from ww w .java2 s.c o m*/ tmpFile = createTempFile(artifactFile); } catch (IOException e) { throw new IllegalStateException(e); } final IStatus result; DigestOutputStream out = null; try { out = new DigestOutputStream( new CopyOutputStream(target, new BufferedOutputStream(new FileOutputStream(tmpFile))), md5Digest); result = this.target.download(toDownload, out, monitor); out.flush(); } catch (IOException e) { deleteQuietly(tmpFile); throw new IllegalStateException(e); } finally { IOUtils.closeQuietly(out); } final String actualMd5 = toHexString(out.getMessageDigest().digest()); if (!md5.equals(actualMd5)) { log.log(LogService.LOG_WARNING, "Unable to cache artifact " + descriptor.getArtifactKey().toExternalForm() + " due to checksum verification failure. Expected " + md5 + " but was " + actualMd5 + "."); deleteQuietly(tmpFile); } else { try { deleteFile(artifactFile); moveFile(tmpFile, artifactFile); } catch (IOException e) { throw new IllegalStateException(e); } } return result; }