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;
}
}
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));
}
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));
}
}
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();
}
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();
}
Aggregations