private boolean isNotFound(final Response response) { return HttpStatus.NOT_FOUND == response.getStatus().getCode(); }
public Builder copy(final Response response) { checkNotNull(response); attributes = response.getAttributes(); headers = response.getHeaders(); status = response.getStatus(); payload = response.getPayload(); return this; }
log.trace("Member {} response {}", member, response.getStatus());
private boolean isValidResponse(final Response response) { return response.getStatus().isSuccessful() || response.getAttributes().contains(USE_DISPATCHED_RESPONSE); } }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { String path = context.getRequest().getPath(); // sanity, to ensure we don't corrupt the path if (!path.endsWith("/")) { path = path + "/"; } for (String file : INDEX_FILES) { Response response = forward(context, path + file); // return response if it was successful or an error which was not not found if (response.getStatus().isSuccessful() || response.getStatus().getCode() != HttpStatus.NOT_FOUND) { return response; } // otherwise try next index file } // or there is no such file, give up not found return HttpResponses.notFound(context.getRequest().getPath()); }
/** * Parses a search response, returning the marshaled {@link NpmSearchResponse}. */ @Nullable private NpmSearchResponse parseSearchResponse(final Repository repository, final Response response) { Payload payload = response.getPayload(); if (response.getStatus().getCode() == HttpStatus.OK && payload != null) { try (InputStream in = payload.openInputStream()) { return npmSearchResponseMapper.readFromInputStream(in); } catch (IOException e) { if (log.isDebugEnabled()) { log.warn("Unable to process search response for repository {}, skipping", repository.getName(), e); } else { log.warn("Unable to process search response for repository {}, cause: {}, skipping", repository.getName(), e.getMessage()); } } } return null; } }
/** * Returns the first OK response from member repositories or {@link HttpResponses#notFound()} if none of the members * responded with OK. */ protected Response getFirst(@Nonnull final Context context, @Nonnull final List<Repository> members, @Nonnull final DispatchedRepositories dispatched) throws Exception { final Request request = context.getRequest(); for (Repository member : members) { log.trace("Trying member: {}", member); // track repositories we have dispatched to, prevent circular dispatch for nested groups if (dispatched.contains(member)) { log.trace("Skipping already dispatched member: {}", member); continue; } dispatched.add(member); final ViewFacet view = member.facet(ViewFacet.class); final Response response = view.dispatch(request, context); log.trace("Member {} response {}", member, response.getStatus()); if (isValidResponse(response)) { return response; } } return notFoundResponse(context); }
LinkedHashMap<Repository, Content> contents = Maps.newLinkedHashMap(); for (Map.Entry<Repository, Response> entry : responses.entrySet()) { if (entry.getValue().getStatus().getCode() == HttpStatus.OK) { Response response = entry.getValue(); if (response.getPayload() instanceof Content) {
@Override protected final Response doGet(@Nonnull final Context context, @Nonnull final GroupHandler.DispatchedRepositories dispatched) throws Exception { Repository repository = context.getRepository(); GroupFacet groupFacet = repository.facet(GroupFacet.class); makeUnconditional(context.getRequest()); Map<Repository, Response> responses; try { responses = getAll(context, groupFacet.members(), dispatched); } finally { makeConditional(context.getRequest()); } List<Payload> payloads = responses.values().stream() .filter(response -> response.getStatus().getCode() == HttpStatus.OK && response.getPayload() != null) .map(Response::getPayload) .collect(Collectors.toList()); if (payloads.isEmpty()) { return notFoundResponse(context); } return HttpResponses.ok(merge(repository, payloads)); }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { Response response = context.proceed(); if (!Boolean.parseBoolean(context.getRequest().getAttributes().get(DO_NOT_REWRITE, String.class))) { if (response.getStatus().getCode() == HttpStatus.OK && response.getPayload() != null) { response = HttpResponses .ok(composerJsonProcessor.rewriteProviderJson(context.getRepository(), response.getPayload())); } } return response; } }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { String action = context.getRequest().getAction(); if (!NFC_CACHEABLE_ACTIONS.contains(action)) { return context.proceed(); } NegativeCacheFacet negativeCache = context.getRepository().facet(NegativeCacheFacet.class); NegativeCacheKey key = negativeCache.getCacheKey(context); Response response; Status status = negativeCache.get(key); if (status == null) { response = context.proceed(); if (isNotFound(response)) { negativeCache.put(key, response.getStatus()); } else if (response.getStatus().isSuccessful()) { negativeCache.invalidate(key); } } else { response = buildResponse(status, context); log.debug("Found {} in negative cache, returning {}", key, response); } return response; }
@Nonnull @Override public Response handle(@Nonnull final Context context) throws Exception { final Response response = context.proceed(); Payload payload = response.getPayload(); if (response.getStatus().isSuccessful() && payload instanceof Content) { final Content content = (Content) payload; final DateTime lastModified = content.getAttributes().get(Content.CONTENT_LAST_MODIFIED, DateTime.class); if (lastModified != null) { response.getHeaders().set(HttpHeaders.LAST_MODIFIED, DateUtils.formatDate(lastModified.toDate())); } final String etag = content.getAttributes().get(Content.CONTENT_ETAG, String.class); if (etag != null) { response.getHeaders().set(HttpHeaders.ETAG, "\"" + etag + "\""); } } return response; } }
if (response.getStatus().getCode() != HttpStatus.OK) {
@Override public Response handle(final Context context) throws Exception { Response response = context.proceed(); if (assetDownloadCountStore.isEnabled() && response != null && response.getStatus().isSuccessful() && isGetRequest(context.getRequest())) { Asset asset = getAssetFromPayload(response.getPayload()); if (asset != null) { assetDownloadCountStore.incrementCount(context.getRepository().getName(), asset.name()); } } return response; }
case HEAD: { final Response response = context.proceed(); if (response.getStatus().isSuccessful() && !requestPredicate.apply(response)) { 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)) {