List of usage examples for java.security DigestInputStream getMessageDigest
public MessageDigest getMessageDigest()
From source file:de.tudarmstadt.ukp.dkpro.core.api.datasets.DatasetFactory.java
/** * Verify/download/update artifact in cache. Execute post-download actions. *///from ww w . j a v a 2s.co m private void materialize(DatasetDescription aDataset) throws IOException { Path root = resolve(aDataset); Collection<ArtifactDescription> artifacts = aDataset.getArtifacts().values(); // First validate if local copies are still up-to-date boolean reload = false; packageValidationLoop: for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (!Files.exists(cachedFile)) { continue; } if (artifact.getSha1() != null) { String actual = getDigest(cachedFile, "SHA1"); if (!artifact.getSha1().equals(actual)) { LOG.info("Local SHA1 hash mismatch on [" + cachedFile + "] - expected [" + artifact.getSha1() + "] - actual [" + actual + "]"); reload = true; break packageValidationLoop; } else { LOG.info("Local SHA1 hash verified on [" + cachedFile + "] - [" + actual + "]"); } } } // If any of the packages are outdated, clear the cache and download again if (reload) { LOG.info("Clearing local cache for [" + root + "]"); FileUtils.deleteQuietly(root.toFile()); } for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); if (Files.exists(cachedFile)) { continue; } if (artifact.getText() != null) { Files.createDirectories(cachedFile.getParent()); LOG.info("Creating [" + cachedFile + "]"); try (Writer out = Files.newBufferedWriter(cachedFile, StandardCharsets.UTF_8)) { out.write(artifact.getText()); } } if (artifact.getUrl() != null) { Files.createDirectories(cachedFile.getParent()); MessageDigest sha1; try { sha1 = MessageDigest.getInstance("SHA1"); } catch (NoSuchAlgorithmException e) { throw new IOException(e); } URL source = new URL(artifact.getUrl()); LOG.info("Fetching [" + cachedFile + "]"); URLConnection connection = source.openConnection(); connection.setRequestProperty("User-Agent", "Java"); try (InputStream is = connection.getInputStream()) { DigestInputStream sha1Filter = new DigestInputStream(is, sha1); Files.copy(sha1Filter, cachedFile); if (artifact.getSha1() != null) { String sha1Hex = new String(Hex.encodeHex(sha1Filter.getMessageDigest().digest())); if (!artifact.getSha1().equals(sha1Hex)) { String message = "SHA1 mismatch. Expected [" + artifact.getSha1() + "] but got [" + sha1Hex + "]."; LOG.error(message); throw new IOException(message); } } } } } // Perform a post-fetch action such as unpacking Path postActionCompleteMarker = resolve(aDataset).resolve(".postComplete"); if (!Files.exists(postActionCompleteMarker)) { for (ArtifactDescription artifact : artifacts) { Path cachedFile = resolve(aDataset, artifact); List<ActionDescription> actions = artifact.getActions(); if (actions != null && !actions.isEmpty()) { try { for (ActionDescription action : actions) { LOG.info("Post-download action [" + action.getAction() + "]"); Class<? extends Action_ImplBase> implClass = actionRegistry.get(action.getAction()); if (implClass == null) { throw new IllegalStateException( "Unknown or unsupported action [" + action.getAction() + "]"); } Action_ImplBase impl = implClass.newInstance(); impl.apply(action, aDataset, artifact, cachedFile); } } catch (IllegalStateException e) { throw e; } catch (IOException e) { throw e; } catch (Exception e) { throw new IllegalStateException(e); } } } Files.createFile(postActionCompleteMarker); } }
From source file:org.calrissian.mango.jms.stream.AbstractJmsFileTransferSupport.java
@SuppressWarnings("unchecked") public void sendStream(Request req, final Destination replyTo) throws IOException { DigestInputStream is;// w w w.ja v a 2 s. c om Assert.notNull(req, "Request cannot be null"); final URI downloadUrl; try { downloadUrl = new URI(req.getDownloadUri()); } catch (URISyntaxException e) { throw new IOException(e); } try { is = new DigestInputStream(new BufferedInputStream(streamOpener.openStream(downloadUrl)), MessageDigest.getInstance(getHashAlgorithm())); } catch (NoSuchAlgorithmException e) { throw new JmsFileTransferException(e); } catch (Throwable e) { logger.info("Error occurred opening stream: " + e); return; } MessageQueueListener queueListener = null; try { @SuppressWarnings("rawtypes") Message returnMessage = (Message) jmsTemplate.execute(new SessionCallback() { @Override public Object doInJms(Session session) throws JMSException { DestinationRequestor requestor = null; try { Message responseMessage = DomainMessageUtils.toResponseMessage(session, new Response(ResponseStatusEnum.ACCEPT)); // Actual file transfer should be done on a queue. // Topics will not work Destination streamTransferDestination = factoryDestination(session, UUID.randomUUID().toString()); requestor = new DestinationRequestor(session, replyTo, streamTransferDestination, jmsTemplate.getReceiveTimeout()); Message returnMessage = requestor.request(responseMessage); requestor.close(); return returnMessage; } finally { if (requestor != null) requestor.close(); } } }, true); // timeout if (returnMessage == null) return; Response response = DomainMessageUtils.fromResponseMessage(returnMessage); // cancel transfer if (!ResponseStatusEnum.STARTSEND.equals(response.getStatus())) return; final Destination receiveAckDestination = returnMessage.getJMSDestination(); final Destination sendDataDestination = returnMessage.getJMSReplyTo(); queueListener = new MessageQueueListener(this, receiveAckDestination); logger.info("Sender[" + req.getRequestId() + "]: Starting send to: " + sendDataDestination); byte[] buffer = new byte[getPieceSize()]; int read = is.read(buffer); long placeInFile = 0; while (read >= 0) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); stream.write(buffer, 0, read); stream.close(); final byte[] pieceData = stream.toByteArray(); final Piece piece = new Piece(placeInFile, pieceData, getHashAlgorithm()); logger.info("Sender[" + req.getRequestId() + "]: Sending piece with position: " + piece.getPosition() + " Size of piece: " + pieceData.length); jmsTemplate.send(sendDataDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { return DomainMessageUtils.toPieceMessage(session, piece); } }); // Message ret = jmsTemplate.receive(receiveAckDestination); Message ret = queueListener.getMessageInQueue(); logger.info("Sender[" + req.getRequestId() + "]: Sent piece and got ack"); // no one on the other end any longer, timeout if (ret == null) return; Response res = DomainMessageUtils.fromResponseMessage(ret); // stop transfer if (ResponseStatusEnum.RESEND.equals(res.getStatus())) { // resend piece logger.info("Sender[" + req.getRequestId() + "]: Resending piece"); } else if (ResponseStatusEnum.DENY.equals(res.getStatus())) { return; } else { buffer = new byte[getPieceSize()]; placeInFile += read; read = is.read(buffer); } } logger.info("Sender[" + req.getRequestId() + "]: Sending stop send"); final DigestInputStream fiIs = is; jmsTemplate.send(sendDataDestination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { Response stopSendResponse = new Response(ResponseStatusEnum.STOPSEND); stopSendResponse.setHash(new String(fiIs.getMessageDigest().digest())); return DomainMessageUtils.toResponseMessage(session, stopSendResponse); } }); Message ackMessage = queueListener.getMessageInQueue(); Object fromMessage = DomainMessageUtils.fromMessage(ackMessage); if (fromMessage instanceof Response) { Response ackResponse = (Response) fromMessage; if (ResponseStatusEnum.RESEND.equals(ackResponse.getStatus())) { // TODO: resend the whole file } } } catch (Exception e) { throw new JmsFileTransferException(e); } finally { try { is.close(); } catch (IOException ignored) { } if (queueListener != null) queueListener.close(); } }
From source file:org.sead.va.dataone.Object.java
@POST @Path("/{objectId}") @Consumes(MediaType.APPLICATION_XML)//from w w w .j a va2 s . c o m @Produces(MediaType.APPLICATION_XML) public Response addObject(@Context HttpServletRequest request, @PathParam("objectId") String id, @QueryParam("creators") String creators, @QueryParam("deprecateFgdc") String deprecateFgdc, String fgdcString) throws UnsupportedEncodingException { Document metaInfo = new Document(); metaInfo.put(Constants.META_FORMAT, "http://www.fgdc.gov/schemas/metadata/fgdc-std-001-1998.xsd"); metaInfo.put(Constants.RO_ID, id); org.w3c.dom.Document doc = null; try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); doc = dBuilder.parse(new ByteArrayInputStream(fgdcString.getBytes())); } catch (ParserConfigurationException e) { System.out.println(e.getMessage()); } catch (SAXException e) { System.out.println(e.getMessage()); } catch (IOException e) { System.out.println(e.getMessage()); } String creator = ""; if (creators != null && !creators.equals("")) { creator = URLEncoder.encode(creators.split("\\|")[0].replace(" ", "").replace(",", "")) + "-"; } String fgdcId = "seadva-" + creator + UUID.randomUUID().toString(); metaInfo.put(Constants.FGDC_ID, fgdcId); final byte[] utf8Bytes = fgdcString.getBytes("UTF-8"); metaInfo.put(Constants.SIZE, utf8Bytes.length); String strDate = simpleDateFormat.format(new Date()); metaInfo.put(Constants.META_UPDATE_DATE, strDate); metaInfo.put(Constants.DEPOSIT_DATE, strDate); try { DigestInputStream digestStream = new DigestInputStream(new ByteArrayInputStream(fgdcString.getBytes()), MessageDigest.getInstance("SHA-1")); if (digestStream.read() != -1) { byte[] buf = new byte[1024]; while (digestStream.read(buf) != -1) ; } byte[] digest = digestStream.getMessageDigest().digest(); metaInfo.put(Constants.FIXITY_FORMAT, "SHA-1"); metaInfo.put(Constants.FIXITY_VAL, new String(Hex.encodeHex(digest))); } catch (IOException e) { e.printStackTrace(); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } Document document = new Document(); document.put(Constants.META_INFO, metaInfo); document.put(Constants.METADATA, fgdcString); RO_STATUS updated = RO_STATUS.NOT_EXIST; updated = deprecateFGDC(id, document); if (deprecateFgdc != null && !deprecateFgdc.equals("") && updated == RO_STATUS.NOT_EXIST) { updated = deprecateFGDC(deprecateFgdc, document); } if (updated == RO_STATUS.NON_IDENTICAL || updated == RO_STATUS.NOT_EXIST) { fgdcCollection.insertOne(document); } return Response.ok().build(); }
From source file:com.mum.app.AutoSubmitPriceApp.java
private String computeContentMD5HeaderValue(InputStream fis) throws IOException, NoSuchAlgorithmException { DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); byte[] buffer = new byte[8192]; while (dis.read(buffer) > 0) ;/* w w w . j a v a 2s .c o m*/ String md5Content = new String( org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())); return md5Content; }
From source file:com.qut.middleware.metadata.source.impl.MetadataSourceBase.java
/** * Reads the provided input stream, calculates and updates an internal hash. * If the internal hash has changed, the byte array obtained from reading the * InputStream is passed to the processMetadata method, along with the * provided MetadataProcessor object.//from www . j a v a 2 s .c om * @param input Input stream to send * @param processor * @throws IOException */ protected void readMetadata(InputStream input, MetadataProcessor processor) throws IOException { byte[] buf = new byte[BUFFER_LENGTH]; long startTime = System.currentTimeMillis(); // Pipe everything through a digest stream so we get a hash value at the end DigestInputStream digestInput = new DigestInputStream(input, this.getMessageDigestInstance()); BufferedInputStream bufferedInput = new BufferedInputStream(digestInput); ByteArrayOutputStream byteOutput = new ByteArrayOutputStream(); this.logger.debug("Metadata source {} - going to read input stream", this.getLocation()); int bytes = 0; while ((bytes = bufferedInput.read(buf)) != -1) { byteOutput.write(buf, 0, bytes); } bufferedInput.close(); digestInput.close(); byteOutput.close(); long endTime = System.currentTimeMillis(); byte[] document = byteOutput.toByteArray(); byte[] hash = digestInput.getMessageDigest().digest(); this.logger.debug("Metadata source {} - read {} bytes of metadata in {} ms", new Object[] { this.getLocation(), document.length, (endTime - startTime) }); // If the document has changed, the hash will be updated, and then we go to process the new document if (this.updateDigest(hash)) { startTime = System.currentTimeMillis(); this.logger.debug("Metadata source {} - updated. Going to process.", this.getLocation()); this.processMetadata(document, processor); endTime = System.currentTimeMillis(); this.logger.info("Metadata source {} - processed document and updated cache in {} ms", this.getLocation(), (endTime - startTime)); } else { this.logger.info("Metadata source {} - has not been updated.", this.getLocation()); } }
From source file:org.mule.module.s3.automation.testcases.CreateObjectTestCases.java
@Ignore @Category({ RegressionTests.class }) @Test/*from w w w .ja va 2s. c o m*/ public void testCreateInputStreamObjectOptionalAttributes() { InputStream inputStream = null; testObjects.putAll((HashMap<String, Object>) context.getBean("createInputStreamObjectTestData")); String host = testObjects.get("host").toString(); String path = testObjects.get("path").toString(); String urlString = String.format("http://%s/%s", host, path); try { URL url = new URL(urlString); URLConnection connection = url.openConnection(); inputStream = connection.getInputStream(); testObjects.put("contentRef", inputStream); MessageDigest messageDigest = MessageDigest.getInstance("MD5"); DigestInputStream digestInputStream = new DigestInputStream(inputStream, messageDigest); byte[] encodedByteData = Base64.encodeBase64(digestInputStream.getMessageDigest().digest()); testObjects.put("contentMd5", new String(encodedByteData, "UTF-8")); testObjects.put("contentLength", Long.valueOf(IOUtils.toByteArray(inputStream).length)); MessageProcessor createObjectFlow = lookupMessageProcessor("create-object-optional-attributes"); MuleEvent response = createObjectFlow.process(getTestEvent(testObjects)); assertEquals("{NullPayload}", response.getMessage().getPayload().toString()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); fail(); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException logOrIgnore) { } } }
From source file:org.dspace.services.impl.storage.DSpaceStorageService.java
@Override public Bitstream store(InputStream input) throws StorageException { this.init();// w w w. j ava 2s . c o m // Create internal ID String id = Utils.generateKey(); Bitstream bitstream = new Bitstream(); bitstream.setDeleted(true); bitstream.setInternalId(id); bitstream.setStoreNumber(incoming); bitstreamDao.save(bitstream); try { GeneralFile file = this.getFile(bitstream); if (file != null && file.getParentFile() != null) file.getParentFile().mkdirs(); file.createNewFile(); GeneralFileOutputStream fos = FileFactory.newFileOutputStream(file); // Read through a digest input stream that will work out the MD5 DigestInputStream dis = null; try { dis = new DigestInputStream(input, MessageDigest.getInstance("MD5")); } catch (NoSuchAlgorithmException nsae) // Should never happen { log.warn("Caught NoSuchAlgorithmException", nsae); } IOUtils.copy(dis, fos); fos.close(); input.close(); bitstream.setSize(file.length()); if (dis != null) { bitstream.setChecksum(Utils.toHex(dis.getMessageDigest().digest())); bitstream.setChecksumAlgorithm("MD5"); } bitstream.setDeleted(false); bitstreamDao.save(bitstream); if (log.isDebugEnabled()) { log.debug("Stored bitstream " + bitstream.getID() + " in file " + file.getAbsolutePath()); } return bitstream; } catch (IOException e) { throw new StorageException(e); } }
From source file:org.dataconservancy.dcs.ingest.client.impl.DualManagerDeposit.java
void uploadFile(DcsFile file, String path) { try {//from ww w . j a v a2 s. c om File physicalFile = new File(path); DigestInputStream digestStream = new DigestInputStream(new FileInputStream(path), MessageDigest.getInstance(checksumAlgorithm)); String mime = MimeUtil.getMostSpecificMimeType(MimeUtil.getMimeTypes(new File(path))).toString(); /* * proceed to the end of the inputStream if we havent got there * apready. We need to visit every byte in order to have calculated * the digest. */ if (digestStream.read() != -1) { byte[] buf = new byte[1024]; while (digestStream.read(buf) != -1) ; } byte[] digest = digestStream.getMessageDigest().digest(); /* Set the file name */ file.setName(physicalFile.getName()); /* Set the calculated fixity */ DcsFixity fixity = new DcsFixity(); fixity.setAlgorithm(checksumAlgorithm); fixity.setValue(new String(Hex.encodeHex(digest))); file.addFixity(fixity); /* Set the format */ DcsFormat format = new DcsFormat(); format.setSchemeUri("http://www.iana.org/assignments/media-types/"); format.setFormat(mime); file.addFormat(format); long length = physicalFile.length(); file.setSource(doUpload(path, mime, digest, length)); file.setSizeBytes(length); file.setExtant(true); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.burlov.amazon.s3.dirsync.DirSync.java
private byte[] digestFile(File file, MessageDigest digest) throws IOException { DigestInputStream in = new DigestInputStream(new FileInputStream(file), digest); IOUtils.copy(in, new NullOutputStream()); in.close();/*ww w. j a v a 2s . c om*/ return in.getMessageDigest().digest(); }
From source file:org.dspace.pack.bagit.Bag.java
public void addData(String relPath, long size, InputStream is) throws IOException { if (filled) { throw new IllegalStateException("Cannot add data to filled bag"); }//from w w w.ja va 2 s . c om // wrap stream in digest stream DigestInputStream dis = null; try { dis = new DigestInputStream(is, MessageDigest.getInstance(CS_ALGO)); FileOutputStream fos = new FileOutputStream(dataFile(relPath)); // attempt to optimize copy in various ways - TODO Utils.copy(dis, fos); fos.close(); dis.close(); is.close(); } catch (NoSuchAlgorithmException nsaE) { throw new IOException("no algorithm: " + CS_ALGO); } // record checksum String brPath = "data/" + relPath; manWriter.writeProperty(Utils.toHex(dis.getMessageDigest().digest()), brPath); }