Search in sources :

Example 26 with IndyClientException

use of org.commonjava.indy.client.core.IndyClientException in project indy by Commonjava.

the class ContentBrowseRemoteIndyListingRewriteManager method rewriteResponse.

public boolean rewriteResponse(HttpServletRequest request, HttpServletResponse response) throws IOException {
    if (!config.isEnabled() || !config.isRemoteIndyListingRewriteEnabled()) {
        logger.debug("[{}] Addon not enabled or not allowed to use remote indy listing rewriting, will not decorate the response by remote indy listing rewriting.", ADDON_NAME);
        return false;
    }
    final String absolutePath = getRequestAbsolutePath(request);
    if (!absolutePath.startsWith("/api/browse/")) {
        logger.debug("[{}] Remote indy listing rewriting: {} is not a content browse request, will not decorate the response. ", ADDON_NAME, absolutePath);
        return false;
    }
    final String httpMethod = request.getMethod();
    if (!httpMethod.equals(HttpMethod.GET) && !httpMethod.equals(HttpMethod.HEAD)) {
        logger.warn("[{}] Directory listing does not support this type of method: {}", ADDON_NAME, httpMethod);
        response.sendError(SC_METHOD_NOT_ALLOWED);
        return true;
    }
    final String pathInfo = request.getPathInfo();
    final Optional<String> originalRepo = RepoProxyUtils.getOriginalStoreKeyFromPath(pathInfo);
    if (!originalRepo.isPresent()) {
        logger.debug("[{}] No matched repo path in request path {}, will not rewrite.", ADDON_NAME, pathInfo);
        return false;
    }
    final String path = RepoProxyUtils.extractPath(absolutePath);
    final String remoteIndyUrl = config.getDefaultRemoteIndyUrl();
    logger.trace("Start to do {} request to remote indy content list for indy {} and path {}", httpMethod, remoteIndyUrl, path);
    try {
        final StoreKey originalStoreKey = StoreKey.fromString(originalRepo.get());
        switch(httpMethod) {
            case HttpMethod.GET:
                return handleGetRequest(remoteIndyUrl, originalStoreKey, path, request, response);
            case HttpMethod.HEAD:
                return handleHeadRequest(originalStoreKey, path, response);
            default:
                logger.warn("[{}] Directory listing does not support this type of method: {}", ADDON_NAME, httpMethod);
                response.sendError(SC_METHOD_NOT_ALLOWED);
                return true;
        }
    } catch (IOException | IndyClientException e) {
        logger.error(String.format("[%s]Error happened during content browse rewriting", ADDON_NAME), e);
        response.sendError(SC_INTERNAL_SERVER_ERROR, e.getMessage());
        return true;
    }
}
Also used : IOException(java.io.IOException) IndyClientException(org.commonjava.indy.client.core.IndyClientException) StoreKey(org.commonjava.indy.model.core.StoreKey)

Example 27 with IndyClientException

use of org.commonjava.indy.client.core.IndyClientException in project indy by Commonjava.

the class ReaonlyHostedDeleteFileTest method deleteFileNotAllowed.

@Test
public void deleteFileNotAllowed() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    InputStream stream = new ByteArrayInputStream(content.getBytes());
    final String path = "/path/to/foo.class";
    final String repoName = "test-hosted";
    HostedRepository repo = new HostedRepository(repoName);
    repo = client.stores().create(repo, name.getMethodName(), HostedRepository.class);
    assertThat(client.content().exists(hosted, repoName, path), equalTo(false));
    client.content().store(hosted, repoName, path, stream);
    assertThat(client.content().exists(hosted, repoName, path), equalTo(true));
    repo.setReadonly(true);
    client.stores().update(repo, name.getMethodName());
    try {
        client.content().delete(hosted, repoName, path);
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(ApplicationStatus.METHOD_NOT_ALLOWED.code()));
    }
    assertThat(client.content().exists(hosted, repoName, path), equalTo(true));
    repo.setReadonly(false);
    client.stores().update(repo, name.getMethodName());
    client.content().delete(hosted, repoName, path);
    assertThat(client.content().exists(hosted, repoName, path), equalTo(false));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientException(org.commonjava.indy.client.core.IndyClientException) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 28 with IndyClientException

use of org.commonjava.indy.client.core.IndyClientException in project indy by Commonjava.

the class RemoteRepoGetErrorTest method run.

@Test
public void run() throws Exception {
    final String repo1 = "repo1";
    final String path = "org/foo/bar/maven-metadata.xml";
    server.registerException(server.formatUrl(repo1, path), "upstream error");
    RemoteRepository remote1 = new RemoteRepository(repo1, server.formatUrl(repo1));
    remote1.setMetadata(Location.CONNECTION_TIMEOUT_SECONDS, Integer.toString(1));
    remote1 = client.stores().create(remote1, "adding remote", RemoteRepository.class);
    try (InputStream is = client.content().get(remote, repo1, path)) {
    } catch (final IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(HttpStatus.SC_BAD_GATEWAY));
    }
}
Also used : InputStream(java.io.InputStream) RemoteRepository(org.commonjava.indy.model.core.RemoteRepository) IndyClientException(org.commonjava.indy.client.core.IndyClientException) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 29 with IndyClientException

use of org.commonjava.indy.client.core.IndyClientException in project indy by Commonjava.

the class NPMReadonlyHostedDeleteFileTest method test.

@Test
public void test() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    InputStream stream = new ByteArrayInputStream(content.getBytes());
    final String path = "jquery/2.1.0";
    final String repoName = "test-npm-hosted";
    HostedRepository repo = new HostedRepository(NPM_PKG_KEY, repoName);
    repo = client.stores().create(repo, "adding npm hosted repo", HostedRepository.class);
    StoreKey storeKey = repo.getKey();
    assertThat(client.content().exists(storeKey, path), equalTo(false));
    client.content().store(storeKey, path, stream);
    assertThat(client.content().exists(storeKey, path), equalTo(true));
    repo.setReadonly(true);
    client.stores().update(repo, "change read-only true");
    try {
        client.content().delete(storeKey, path);
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(ApplicationStatus.METHOD_NOT_ALLOWED.code()));
    }
    assertThat(client.content().exists(storeKey, path), equalTo(true));
    repo.setReadonly(false);
    client.stores().update(repo, "change read-only false");
    client.content().delete(storeKey, path);
    assertThat(client.content().exists(storeKey, path), equalTo(false));
    stream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientException(org.commonjava.indy.client.core.IndyClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Example 30 with IndyClientException

use of org.commonjava.indy.client.core.IndyClientException in project indy by Commonjava.

the class NPMReadonlyHostedStoreFileTest method test.

@Test
public void test() throws Exception {
    final String content = "This is a test: " + System.nanoTime();
    InputStream stream = new ByteArrayInputStream(content.getBytes());
    final String path = "jquery/-/jquery-2.1.0.tgz";
    final String repoName = "test-hosted";
    HostedRepository repo = new HostedRepository(NPM_PKG_KEY, repoName);
    repo.setReadonly(true);
    repo = client.stores().create(repo, "adding npm hosted repo", HostedRepository.class);
    StoreKey storeKey = repo.getKey();
    assertThat(client.content().exists(storeKey, path), equalTo(false));
    try {
        client.content().store(storeKey, path, stream);
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(ApplicationStatus.METHOD_NOT_ALLOWED.code()));
    }
    assertThat(client.content().exists(storeKey, path), equalTo(false));
    repo.setReadonly(false);
    client.stores().update(repo, "change read-only false");
    stream = new ByteArrayInputStream(content.getBytes());
    client.content().store(storeKey, path, stream);
    assertThat(client.content().exists(storeKey, path), equalTo(true));
    final InputStream is = client.content().get(storeKey, path);
    final String result = IOUtils.toString(is);
    assertThat(result, equalTo(content));
    is.close();
    stream.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IndyClientException(org.commonjava.indy.client.core.IndyClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository) Test(org.junit.Test) AbstractContentManagementTest(org.commonjava.indy.ftest.core.AbstractContentManagementTest)

Aggregations

IndyClientException (org.commonjava.indy.client.core.IndyClientException)31 Test (org.junit.Test)21 InputStream (java.io.InputStream)17 AbstractContentManagementTest (org.commonjava.indy.ftest.core.AbstractContentManagementTest)13 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)12 HostedRepository (org.commonjava.indy.model.core.HostedRepository)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 StoreKey (org.commonjava.indy.model.core.StoreKey)9 IOException (java.io.IOException)6 HttpResources (org.commonjava.indy.client.core.helper.HttpResources)5 HttpResponse (org.apache.http.HttpResponse)3 StatusLine (org.apache.http.StatusLine)3 AbstractIndyFunctionalTest (org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest)3 Logger (org.slf4j.Logger)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ArrayList (java.util.ArrayList)2 CountingInputStream (org.apache.commons.io.input.CountingInputStream)2 HttpGet (org.apache.http.client.methods.HttpGet)2 HttpPost (org.apache.http.client.methods.HttpPost)2 IndyClientHttp (org.commonjava.indy.client.core.IndyClientHttp)2