Example usage for java.security DigestOutputStream close

List of usage examples for java.security DigestOutputStream close

Introduction

In this page you can find the example usage for java.security DigestOutputStream close.

Prototype

@Override
public void close() throws IOException 

Source Link

Document

Closes this output stream and releases any system resources associated with the stream.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Security.addProvider(new BouncyCastleProvider());
    byte[] input = "www.java2s.com".getBytes();
    System.out.println("input     : " + new String(input));
    MessageDigest hash = MessageDigest.getInstance("SHA1");

    ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(input);
    DigestInputStream digestInputStream = new DigestInputStream(byteArrayInputStream, hash);
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    int ch;/*from  w  w  w  . j a v a  2  s.c o  m*/
    while ((ch = digestInputStream.read()) >= 0) {
        byteArrayOutputStream.write(ch);
    }

    byte[] newInput = byteArrayOutputStream.toByteArray();
    System.out.println("in digest : " + new String(digestInputStream.getMessageDigest().digest()));

    byteArrayOutputStream = new ByteArrayOutputStream();
    DigestOutputStream digestOutputStream = new DigestOutputStream(byteArrayOutputStream, hash);
    digestOutputStream.write(newInput);
    digestOutputStream.close();

    System.out.println("out digest: " + new String(digestOutputStream.getMessageDigest().digest()));
}

From source file:alluxio.proxy.s3.S3RestServiceHandler.java

/**
 * @summary uploads an object or part of an object in multipart upload
 * @param contentMD5 the optional Base64 encoded 128-bit MD5 digest of the object
 * @param bucket the bucket name/*from   ww  w. j a va 2s .c  o m*/
 * @param object the object name
 * @param partNumber the identification of the part of the object in multipart upload,
 *                   otherwise null
 * @param uploadId the upload ID of the multipart upload, otherwise null
 * @param is the request body
 * @return the response object
 */
@PUT
@Path(OBJECT_PARAM)
@ReturnType("java.lang.Void")
@Consumes(MediaType.WILDCARD)
public Response createObjectOrUploadPart(@HeaderParam("Content-MD5") final String contentMD5,
        @PathParam("bucket") final String bucket, @PathParam("object") final String object,
        @QueryParam("partNumber") final Integer partNumber, @QueryParam("uploadId") final Long uploadId,
        final InputStream is) {
    return S3RestUtils.call(bucket, new S3RestUtils.RestCallable<Response>() {
        @Override
        public Response call() throws S3Exception {
            Preconditions.checkNotNull(bucket, "required 'bucket' parameter is missing");
            Preconditions.checkNotNull(object, "required 'object' parameter is missing");
            Preconditions.checkArgument(
                    (partNumber == null && uploadId == null) || (partNumber != null && uploadId != null),
                    "'partNumber' and 'uploadId' parameter should appear together or be "
                            + "missing together.");

            String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket);
            checkBucketIsAlluxioDirectory(bucketPath);

            String objectPath = bucketPath + AlluxioURI.SEPARATOR + object;
            if (partNumber != null) {
                // This object is part of a multipart upload, should be uploaded into the temporary
                // directory first.
                String tmpDir = S3RestUtils.getMultipartTemporaryDirForObject(bucketPath, object);
                checkUploadId(new AlluxioURI(tmpDir), uploadId);
                objectPath = tmpDir + AlluxioURI.SEPARATOR + Integer.toString(partNumber);
            }
            AlluxioURI objectURI = new AlluxioURI(objectPath);

            try {
                CreateFileOptions options = CreateFileOptions.defaults().setRecursive(true)
                        .setWriteType(getS3WriteType());
                FileOutStream os = mFileSystem.createFile(objectURI, options);
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                DigestOutputStream digestOutputStream = new DigestOutputStream(os, md5);

                try {
                    ByteStreams.copy(is, digestOutputStream);
                } finally {
                    digestOutputStream.close();
                }

                byte[] digest = md5.digest();
                String base64Digest = BaseEncoding.base64().encode(digest);
                if (contentMD5 != null && !contentMD5.equals(base64Digest)) {
                    // The object may be corrupted, delete the written object and return an error.
                    try {
                        mFileSystem.delete(objectURI);
                    } catch (Exception e2) {
                        // intend to continue and return BAD_DIGEST S3Exception.
                    }
                    throw new S3Exception(objectURI.getPath(), S3ErrorCode.BAD_DIGEST);
                }

                String entityTag = Hex.encodeHexString(digest);
                return Response.ok().tag(entityTag).build();
            } catch (Exception e) {
                throw toObjectS3Exception(e, objectPath);
            }
        }
    });
}

From source file:alluxio.proxy.s3.S3RestServiceHandler.java

private Response completeMultipartUpload(final String bucket, final String object, final long uploadId) {
    return S3RestUtils.call(bucket, new S3RestUtils.RestCallable<CompleteMultipartUploadResult>() {
        @Override/* www. j  av a  2s. co m*/
        public CompleteMultipartUploadResult call() throws S3Exception {
            String bucketPath = parseBucketPath(AlluxioURI.SEPARATOR + bucket);
            checkBucketIsAlluxioDirectory(bucketPath);
            String objectPath = bucketPath + AlluxioURI.SEPARATOR + object;
            AlluxioURI multipartTemporaryDir = new AlluxioURI(
                    S3RestUtils.getMultipartTemporaryDirForObject(bucketPath, object));
            checkUploadId(multipartTemporaryDir, uploadId);

            try {
                List<URIStatus> parts = mFileSystem.listStatus(multipartTemporaryDir);
                Collections.sort(parts, new URIStatusNameComparator());

                CreateFileOptions options = CreateFileOptions.defaults().setRecursive(true)
                        .setWriteType(getS3WriteType());
                FileOutStream os = mFileSystem.createFile(new AlluxioURI(objectPath), options);
                MessageDigest md5 = MessageDigest.getInstance("MD5");
                DigestOutputStream digestOutputStream = new DigestOutputStream(os, md5);

                try {
                    for (URIStatus part : parts) {
                        try (FileInStream is = mFileSystem.openFile(new AlluxioURI(part.getPath()))) {
                            ByteStreams.copy(is, digestOutputStream);
                        }
                    }
                } finally {
                    digestOutputStream.close();
                }

                mFileSystem.delete(multipartTemporaryDir, DeleteOptions.defaults().setRecursive(true));

                String entityTag = Hex.encodeHexString(md5.digest());
                return new CompleteMultipartUploadResult(objectPath, bucket, object, entityTag);
            } catch (Exception e) {
                throw toObjectS3Exception(e, objectPath);
            }
        }
    });
}

From source file:com.jpeterson.littles3.StorageEngine.java

/**
 * Write//from   www. j  a v a  2 s. c  o  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:nu.kelvin.jfileshare.ajax.FileReceiverServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession();
    UserItem currentUser = (UserItem) session.getAttribute("user");
    if (currentUser != null && ServletFileUpload.isMultipartContent(req)) {
        Conf conf = (Conf) getServletContext().getAttribute("conf");
        // keep files of up to 10 MiB in memory 10485760
        FileItemFactory factory = new DiskFileItemFactory(10485760, new File(conf.getPathTemp()));
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(conf.getFileSizeMax());

        // set file upload progress listener
        FileUploadListener listener = new FileUploadListener();
        session.setAttribute("uploadListener", listener);
        upload.setProgressListener(listener);

        File tempFile = File.createTempFile(String.format("%05d-", currentUser.getUid()), null,
                new File(conf.getPathTemp()));
        tempFile.deleteOnExit();//w w w. j a v a 2s.co m
        try {
            FileItem file = new FileItem();

            /* iterate over all uploaded items */
            FileItemIterator it = upload.getItemIterator(req);
            FileOutputStream filestream = null;

            while (it.hasNext()) {
                FileItemStream item = it.next();
                String name = item.getFieldName();
                InputStream instream = item.openStream();
                DigestOutputStream outstream = null;

                if (item.isFormField()) {
                    String value = Streams.asString(instream);
                    // logger.info(name + " : " + value);
                    /* not the file upload. Maybe the password field? */
                    if (name.equals("password") && !value.equals("")) {
                        logger.info("Uploaded file has password set");
                        file.setPwPlainText(value);
                    }
                    instream.close();
                } else {
                    // This is the file you're looking for
                    file.setName(item.getName());
                    file.setType(
                            item.getContentType() == null ? "application/octet-stream" : item.getContentType());
                    file.setUid(currentUser.getUid());

                    try {
                        filestream = new FileOutputStream(tempFile);
                        MessageDigest md = MessageDigest.getInstance("MD5");
                        outstream = new DigestOutputStream(filestream, md);
                        long filesize = IOUtils.copyLarge(instream, outstream);

                        if (filesize == 0) {
                            throw new Exception("File is empty.");
                        }
                        md = outstream.getMessageDigest();
                        file.setMd5sum(toHex(md.digest()));
                        file.setSize(filesize);

                    } finally {
                        if (outstream != null) {
                            try {
                                outstream.close();
                            } catch (IOException ignored) {
                            }
                        }
                        if (filestream != null) {
                            try {
                                filestream.close();
                            } catch (IOException ignored) {
                            }
                        }
                        if (instream != null) {
                            try {
                                instream.close();
                            } catch (IOException ignored) {
                            }
                        }
                    }
                }
            }
            /* All done. Save the new file */
            if (conf.getDaysFileExpiration() != 0) {
                file.setDaysToKeep(conf.getDaysFileExpiration());
            }
            if (file.create(ds, req.getRemoteAddr())) {
                File finalFile = new File(conf.getPathStore(), Integer.toString(file.getFid()));
                tempFile.renameTo(finalFile);
                logger.log(Level.INFO, "User {0} storing file \"{1}\" in the filestore",
                        new Object[] { currentUser.getUid(), file.getName() });
                req.setAttribute("msg",
                        "File <strong>\"" + Helpers.htmlSafe(file.getName())
                                + "\"</strong> uploaded successfully. <a href='" + req.getContextPath()
                                + "/file/edit/" + file.getFid() + "'>Click here to edit file</a>");
                req.setAttribute("javascript", "parent.uploadComplete('info');");
            } else {
                req.setAttribute("msg", "Unable to contact the database");
                req.setAttribute("javascript", "parent.uploadComplete('critical');");
            }
        } catch (SizeLimitExceededException e) {
            tempFile.delete();
            req.setAttribute("msg", "File is too large. The maximum size of file uploads is "
                    + FileItem.humanReadable(conf.getFileSizeMax()));
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } catch (FileUploadException e) {
            tempFile.delete();
            req.setAttribute("msg", "Unable to upload file");
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } catch (Exception e) {
            tempFile.delete();
            req.setAttribute("msg",
                    "Unable to upload file. ".concat(e.getMessage() == null ? "" : e.getMessage()));
            req.setAttribute("javascript", "parent.uploadComplete('warning');");
        } finally {
            session.setAttribute("uploadListener", null);
        }
        ServletContext app = getServletContext();
        RequestDispatcher disp = app.getRequestDispatcher("/templates/AjaxDummy.jsp");
        disp.forward(req, resp);
    }
}

From source file:org.dataconservancy.dcs.lineage.http.support.RequestUtil.java

/**
 * Calculates a MD5 digest over the list of entity ids, suitable for use as an
 * ETag.  If the list is empty, {@code null} is returned.
 *
 * @param entityIds the entities//from  w w  w.ja v  a 2  s . c  o m
 * @return the MD5 digest, or {@code null} if {@code entityIds} is empty.
 */
public static String calculateDigest(List<String> entityIds) {
    if (entityIds.isEmpty()) {
        return null;
    }

    NullOutputStream nullOut = new NullOutputStream();
    DigestOutputStream digestOut = null;

    try {
        digestOut = new DigestOutputStream(nullOut, MessageDigest.getInstance("MD5"));
        for (String id : entityIds) {
            IOUtils.write(id, digestOut);
        }
        digestOut.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return digestToHexString(digestOut.getMessageDigest().digest());
}

From source file:org.dataconservancy.dcs.util.http.ETagCalculator.java

public static String calculate(String id) {
    if (id.isEmpty()) {
        return null;
    }//w  w w . ja  v  a  2  s . c  o  m

    NullOutputStream nullOut = new NullOutputStream();
    DigestOutputStream digestOut = null;

    try {
        digestOut = new DigestOutputStream(nullOut, MessageDigest.getInstance("MD5"));
        IOUtils.write(id, digestOut);
        digestOut.close();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e.getMessage(), e);
    }

    return digestToHexString(digestOut.getMessageDigest().digest());
}

From source file:org.geogit.storage.AbstractObjectDatabase.java

/**
 * @see org.geogit.storage.ObjectDatabase#put(org.geogit.storage.ObjectWriter)
 *///from  ww w .  j  a  va2s.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;
}