Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package io.packagecloud.client;
  
  import  org.apache.commons.io.IOUtils;
 
 import java.util.Map;
 
 public class Client implements io.packagecloud.client.interfaces.Client {
 
     private static Logger logger = LoggerProvider.getLogger();
 
     private final HttpHost targetHost;
     private final CloseableHttpClient httpClient = HttpClients.createDefault();
     private final HttpClientContext context = HttpClientContext.create();
     private final SecureRandom random = new SecureRandom();
     private final Credentials credentials;
 
     public Client(Credentials credentials) {
         this(credentialsnew Connection());
     }
 
     public Client(Credentials credentialsConnection connection) {
         this. = credentials;
 
         this. = new HttpHost(
                 connection.getHost(),
                 connection.getPort(),
                 connection.getScheme());
 
         AuthCache authCache = new BasicAuthCache();
         BasicScheme basicAuth = new BasicScheme();
         authCache.put(basicAuth);
         CredentialsProvider credsProvider = new BasicCredentialsProvider();
         credsProvider.setCredentials(
                 new AuthScope(..."basic"),
                 new UsernamePasswordCredentials(getToken(), ""));
         .setCredentialsProvider(credsProvider);
         .setAuthCache(authCache);
     }
 
     public HttpHost getTargetHost() {
         return ;
     }
 
     public Credentials getCredentials() {
         return ;
     }
 
     public String getToken() {
         return this.getCredentials().getToken();
     }
     public String getUsername() {
         return this.getCredentials().getUsername();
     }
 
     @Override
     public Result getDistributions()
             throws UnauthorizedExceptionIOExceptionServerErrorException {
         String strResponse;
         StatusLine statusLine;
 
         HttpGet httpGet = new HttpGet("/api/v1/distributions.json");
 
         CloseableHttpResponse response = .execute(
                 httpGet);
         try {
             statusLine = response.getStatusLine();
             HttpEntity entity = response.getEntity();
             strResponse = EntityUtils.toString(entity);
             .info(response.getStatusLine().toString());
         } finally {
             response.close();
         }
 
        return getResultOrThrow(strResponsestatusLine);
    }
    @Override
    public Result packageContents(InputStream fileStreamString filenameString repository)
            throws UnauthorizedExceptionIOExceptionServerErrorException {
        String strResponse;
        StatusLine statusLine;
        String url = String.format("/api/v1/repos/%s/%s/packages/contents"getUsername(), repository);
        HttpPost httppost = new HttpPost(url);
        ByteArrayBody body = bodyFromInputStream(fileStreamfilename);
        HttpEntity reqEntity = MultipartEntityBuilder.create()
                .addPart("package[package_file]"body)
                .build();
        httppost.setEntity(reqEntity);
        CloseableHttpResponse response = .execute(
                httppost);
        try {
            statusLine = response.getStatusLine();
            HttpEntity entity = response.getEntity();
            strResponse = EntityUtils.toString(entity);
            .info(response.getStatusLine().toString());
        } finally {
            response.close();
        }
        return getResultOrThrow(strResponsestatusLine);
    }
    @Override
    public Result putPackage(
            InputStream fileStream,
            String filename,
            String repository,
            Integer distroVersionId,
            Map<StringInputStreamsourceFiles)
        String strResponse;
        StatusLine statusLine;
        String url = String.format("/api/v1/repos/%s/%s/packages.json"getUsername(), repository);
        HttpPost httppost = new HttpPost(url);
        ByteArrayBody body = bodyFromInputStream(fileStreamgenerateFilename());
        MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create()
                .addPart("package[package_file]"body);
        if (distroVersionId != null) {
            StringBody distro = new StringBody(String.valueOf(distroVersionId), .);
            reqEntity.addPart("package[distro_version_id]"distro);
        }
        if (sourceFiles != null) {
            for (String key : sourceFiles.keySet()) {
                .fine(String.format("Processing SourceFile part: %s"key));
                InputStream sourceStream = sourceFiles.get(key);
                ByteArrayBody sourceFileBody = bodyFromInputStream(sourceStreamkey);
                reqEntity.addPart("package[source_files][]"sourceFileBody);
            }
        }
        httppost.setEntity(reqEntity.build());
        CloseableHttpResponse response = .execute(
                httppost);
        try {
            statusLine = response.getStatusLine();
            HttpEntity entity = response.getEntity();
            strResponse = EntityUtils.toString(entity);
            .info(response.getStatusLine().toString());
        } finally {
            response.close();
        }
        return getResultOrThrow(strResponsestatusLine);
    }
    private Result getResultOrThrow(String strResponseStatusLine statusLine)
            throws UnauthorizedExceptionServerErrorException {
        if (statusLine != null && (statusLine.getStatusCode() == 200 || statusLine.getStatusCode() == 201)){
            return new Result(strResponse);
        } else if(statusLine != null && statusLine.getStatusCode() == 401) {
            throw new UnauthorizedException();
        } else {
            String exception = String.format("%s:\n%s"statusLinestrResponse);
            throw new ServerErrorException(exception);
        }
    }
    private ByteArrayBody bodyFromInputStream(InputStream isString filenamethrows IOException {
        byte[] bytes = IOUtils.toByteArray(is);
        return new ByteArrayBody(bytesfilename);
    }
    private String generateFilename() {
        return new BigInteger(130, ).toString(32);
    }
New to GrepCode? Check out our FAQ X