<commit_before>package org.carlspring.strongbox.rest;

import org.carlspring.maven.commons.util.ArtifactUtils;
import org.carlspring.strongbox.io.MultipleDigestInputStream;
import org.carlspring.strongbox.resource.ResourceCloser;
import org.carlspring.strongbox.security.encryption.EncryptionConstants;
import org.carlspring.strongbox.security.jaas.authentication.AuthenticationException;
import org.carlspring.strongbox.storage.checksum.ChecksumCacheManager;
import org.carlspring.strongbox.storage.resolvers.ArtifactResolutionException;
import org.carlspring.strongbox.storage.resolvers.ArtifactResolutionService;
import org.carlspring.strongbox.util.MessageDigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author <NAME>
 */
@Component
@Path("/storages")
public class ArtifactRestlet
        extends BaseRestlet
{

    private static final Logger logger = LoggerFactory.getLogger(ArtifactRestlet.class);

    @Autowired
    private ChecksumCacheManager checksumCacheManager;

    @Autowired
    private ArtifactResolutionService artifactResolutionService;


    @PUT
    @Path("{storage}/{repository}/{path:.*}")
    public Response upload(@PathParam("storage") String storage,
                           @PathParam("repository") String repository,
                           @PathParam("path") String path,
                           @Context HttpHeaders headers,
                           @Context HttpServletRequest request,
                           InputStream is)
            throws IOException,
                   AuthenticationException,
                   NoSuchAlgorithmException
    {
        String protocol = request.getRequestURL().toString().split(":")[0];

        handleAuthentication(storage, repository, path, headers, protocol);

        boolean fileIsChecksum = path.endsWith(".md5") || path.endsWith(".sha1");
        MultipleDigestInputStream mdis = new MultipleDigestInputStream(is, new String[]{ EncryptionConstants.ALGORITHM_MD5,
                                                                                         EncryptionConstants.ALGORITHM_SHA1 });

        // TODO: Do something with the artifact
        // Repository r = getDataCenter().getStorage(storage).getRepository(repository);
        // TODO: If the repository's type is In-Memory, do nothing.
        // TODO: For all other type of repositories, invoke the respective storage provider.

        // TODO: If this is not a checksum file, store the file.
        // TODO: If this is a checksum file, keep the hash in a String.
        ByteArrayOutputStream baos = null;
        if (fileIsChecksum)
        {
            baos = new ByteArrayOutputStream();
        }

        OutputStream os = null;
        try
        {
            os = artifactResolutionService.getOutputStream(repository, path);

            int readLength;
            byte[] bytes = new byte[4096];

            while ((readLength = mdis.read(bytes, 0, bytes.length)) != -1)
            {
                /*
                if (fileIsChecksum)
                {
                    // Buffer the checksum for later validation
                    baos.write(bytes, 0, readLength);
                    baos.flush();
                }
                else
                {
                */
                    os.write(bytes, 0, readLength);
                    os.flush();
                    // Write the artifact
                //}
            }

            System.out.println("# Wrote " + readLength + " bytes.");
        }
        catch (ArtifactResolutionException e)
        {
            e.printStackTrace();
            throw new WebApplicationException(e, Response.Status.NOT_FOUND);
        }
        finally
        {
            ResourceCloser.close(os, logger);
        }

        final String artifactPath = storage + "/" + repository + "/" + path;
        if (!fileIsChecksum)
        {
            addChecksumsToCacheManager(mdis, artifactPath);
        }
        else
        {
            validateUploadedChecksumAgainstCache(baos, artifactPath);
        }

        return Response.ok().build();
    }
<commit_msg>Polishing up some old code.<commit_after>package org.carlspring.strongbox.rest;

import org.carlspring.maven.commons.util.ArtifactUtils;
import org.carlspring.strongbox.io.MultipleDigestInputStream;
import org.carlspring.strongbox.resource.ResourceCloser;
import org.carlspring.strongbox.security.encryption.EncryptionConstants;
import org.carlspring.strongbox.security.jaas.authentication.AuthenticationException;
import org.carlspring.strongbox.storage.checksum.ChecksumCacheManager;
import org.carlspring.strongbox.storage.resolvers.ArtifactResolutionException;
import org.carlspring.strongbox.storage.resolvers.ArtifactResolutionService;
import org.carlspring.strongbox.util.MessageDigestUtils;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Response;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author <NAME>
 */
@Component
@Path("/storages")
public class ArtifactRestlet
        extends BaseRestlet
{

    private static final Logger logger = LoggerFactory.getLogger(ArtifactRestlet.class);

    @Autowired
    private ChecksumCacheManager checksumCacheManager;

    @Autowired
    private ArtifactResolutionService artifactResolutionService;


    @PUT
    @Path("{storage}/{repository}/{path:.*}")
    public Response upload(@PathParam("storage") String storage,
                           @PathParam("repository") String repository,
                           @PathParam("path") String path,
                           @Context HttpHeaders headers,
                           @Context HttpServletRequest request,
                           InputStream is)
            throws IOException,
                   AuthenticationException,
                   NoSuchAlgorithmException
    {
        String protocol = request.getRequestURL().toString().split(":")[0];

        handleAuthentication(storage, repository, path, headers, protocol);

        boolean fileIsChecksum = path.endsWith(".md5") || path.endsWith(".sha1");
        MultipleDigestInputStream mdis = new MultipleDigestInputStream(is, new String[]{ EncryptionConstants.ALGORITHM_MD5,
                                                                                         EncryptionConstants.ALGORITHM_SHA1 });

        // TODO: If this is not a checksum file, store the file.
        // TODO: If this is a checksum file, keep the hash in a String.
        ByteArrayOutputStream baos = null;
        if (fileIsChecksum)
        {
            baos = new ByteArrayOutputStream();
        }

        OutputStream os = null;
        try
        {
            os = artifactResolutionService.getOutputStream(repository, path);

            int readLength;
            byte[] bytes = new byte[4096];

            while ((readLength = mdis.read(bytes, 0, bytes.length)) != -1)
            {
                if (fileIsChecksum)
                {
                    // Buffer the checksum for later validation
                    baos.write(bytes, 0, readLength);
                    baos.flush();
                }
                else
                {
                    os.write(bytes, 0, readLength);
                    os.flush();
                    // Write the artifact
                }
            }

            System.out.println("# Wrote " + readLength + " bytes.");
        }
        catch (ArtifactResolutionException e)
        {
            e.printStackTrace();
            throw new WebApplicationException(e, Response.Status.NOT_FOUND);
        }
        finally
        {
            ResourceCloser.close(os, logger);
        }

        final String artifactPath = storage + "/" + repository + "/" + path;
        if (!fileIsChecksum)
        {
            addChecksumsToCacheManager(mdis, artifactPath);
        }
        else
        {
            validateUploadedChecksumAgainstCache(baos, artifactPath);
        }

        return Response.ok().build();
    }

{'fraction_non_alphanumeric': 0.09138761339455706, 'fraction_numerical': 0.0033598387277410683, 'mean_word_length': 2.1768054073283527, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4559195', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2245, 'n_words': 472}