private Content generatePackagesJson(final Context context) throws IOException { try { // TODO: Better logging and error checking on failure/non-200 scenarios Request request = new Request.Builder().action(GET).path("/" + LIST_JSON).build(); Response response = getRepository().facet(ViewFacet.class).dispatch(request, context); Payload payload = checkNotNull(response.getPayload()); return composerJsonProcessor.generatePackagesFromList(getRepository(), payload); } catch (IOException e) { throw new UncheckedIOException(e); } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } }
private String getZipballUrl(final Context context) { try { TokenMatcher.State state = context.getAttributes().require(TokenMatcher.State.class); Map<String, String> tokens = state.getTokens(); String vendor = tokens.get(VENDOR_TOKEN); String project = tokens.get(PROJECT_TOKEN); String version = tokens.get(VERSION_TOKEN); Request request = new Request.Builder().action(GET).path("/" + buildProviderPath(vendor, project)) .attribute(ComposerProviderHandler.DO_NOT_REWRITE, "true").build(); Response response = getRepository().facet(ViewFacet.class).dispatch(request, context); Payload payload = response.getPayload(); if (payload == null) { throw new NonResolvableProviderJsonException( String.format("No provider found for vendor %s, project %s, version %s", vendor, project, version)); } else { return composerJsonProcessor.getDistUrl(vendor, project, version, payload); } } catch (IOException e) { throw new UncheckedIOException(e); } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new RuntimeException(e); } }
@Nonnull @Override public Content searchIndex(@Nullable final DateTime since) throws IOException { try { final Request getRequest = new Request.Builder() .action(GET) .path("/" + NpmFacetUtils.REPOSITORY_ROOT_ASSET) .build(); Context context = new Context(getRepository(), getRequest); context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_INDEX); Content fullIndex = getRepository().facet(ProxyFacet.class).get(context); if (fullIndex == null) { throw new IOException("Could not retrieve registry root"); } return NpmSearchIndexFilter.filterModifiedSince(fullIndex, since); } catch (Exception e) { throw new IOException(e); } }
case PUT: case DELETE: { final Request getRequest = new Request.Builder().copy(context.getRequest()).action(GET).build(); final Response response = context.getRepository().facet(ViewFacet.class).dispatch(getRequest); if (response.getStatus().isSuccessful() && !requestPredicate.apply(response)) {
/** * This method MUST NOT be called from within a TX, as it dispatches a new request! It fails with * {@code java.lang.IllegalStateException}: "Transaction already in progress" otherwise! */ private NestedAttributesMap retrievePackageRoot(final NpmPackageId packageId, final Context context) throws IOException { try { Request getRequest = new Request.Builder().action(GET).path("/" + packageId.id()).build(); Response response = getRepository().facet(ViewFacet.class).dispatch(getRequest, context); if (response.getPayload() == null) { throw new IOException("Could not retrieve package " + packageId); } final InputStream packageRootIn = response.getPayload().openInputStream(); return NpmFacetUtils.parse(() -> packageRootIn); } catch (IOException e) { throw e; } catch (Exception e) { Throwables.throwIfUnchecked(e); throw new IOException(e); } }
@Override public Content searchV1(final Parameters parameters) throws IOException { try { final Request getRequest = new Request.Builder() .action(GET) .path("/" + NpmFacetUtils.REPOSITORY_SEARCH_ASSET) .parameters(parameters) .build(); Context context = new Context(getRepository(), getRequest); context.getAttributes().set(ProxyTarget.class, ProxyTarget.SEARCH_V1_RESULTS); Content searchResults = getRepository().facet(ProxyFacet.class).get(context); if (searchResults == null) { throw new IOException("Could not retrieve registry search"); } return searchResults; } catch (Exception e) { throw new IOException(e); } } }
/** * Primes proxy cache with given path and return {@code true} if succeeds. Accepts only maven proxy type. */ private static boolean prefetch(final Repository repository, final String path) throws IOException { MavenPath mavenPath = repository.facet(MavenFacet.class).getMavenPathParser().parsePath(path); Request getRequest = new Request.Builder() .action(GET) .path(path) .build(); Context context = new Context(repository, getRequest); context.getAttributes().set(MavenPath.class, mavenPath); return repository.facet(ProxyFacet.class).get(context) != null; }
private Response forward(final Context context, final String path) throws Exception { log.trace("Forwarding request to path: {}", path); Request request = new Request.Builder() .copy(context.getRequest()) .path(path) .build(); return context.getRepository() .facet(ViewFacet.class) .dispatch(request); } }