@Test( expected = RedirectionException.class ) public void downloadLatestVersion() throws Exception { String id = createAndScanRepo(); try { Response response = getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "LATEST", null, null ); } catch ( RedirectionException e ) { Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id + "/org/apache/archiva/archiva-test/2.0/archiva-test-2.0.jar" ) ) ).isEqualTo( 0 ); throw e; } finally { getManagedRepositoriesService().deleteManagedRepository( id, false ); } }
public static Class<? extends WebApplicationException> getWebApplicationExceptionClass(Response response) { int status = response.getStatus(); final Class<? extends WebApplicationException> exceptionType = EXCEPTIONS_MAP.get(status); if (exceptionType == null) { final int family = status / 100; switch (family) { case 3: return RedirectionException.class; case 4: return ClientErrorException.class; case 5: return ServerErrorException.class; default: return WebApplicationException.class; } } return exceptionType; }
@Test public void testCreateException() { assertExceptionType(Response.Status.INTERNAL_SERVER_ERROR, InternalServerErrorException.class); assertExceptionType(Response.Status.NOT_FOUND, NotFoundException.class); assertExceptionType(Response.Status.FORBIDDEN, ForbiddenException.class); assertExceptionType(Response.Status.BAD_REQUEST, BadRequestException.class); assertExceptionType(Response.Status.METHOD_NOT_ALLOWED, NotAllowedException.class); assertExceptionType(Response.Status.UNAUTHORIZED, NotAuthorizedException.class); assertExceptionType(Response.Status.NOT_ACCEPTABLE, NotAcceptableException.class); assertExceptionType(Response.Status.UNSUPPORTED_MEDIA_TYPE, NotSupportedException.class); assertExceptionType(Response.Status.SERVICE_UNAVAILABLE, ServiceUnavailableException.class); assertExceptionType(Response.Status.TEMPORARY_REDIRECT, RedirectionException.class); assertExceptionType(Response.Status.LENGTH_REQUIRED, ClientErrorException.class); assertExceptionType(Response.Status.BAD_GATEWAY, ServerErrorException.class); assertExceptionType(Response.Status.NO_CONTENT, WebApplicationException.class); }
@GET @Path("redirect/{status}") public Response redirect(@PathParam("status") int status) { if (status == 307) { throw new RedirectionException(status, URI.create("hello")); } return Response.status(status).header("Location", "/hello").build(); }
@Test( expected = RedirectionException.class ) public void downloadFixedVersion() throws Exception { String id = createAndScanRepo(); try { Response response = getSearchService().redirectToArtifactFile( null, "org.apache.archiva", "archiva-test", "1.0", null, null ); } catch ( RedirectionException e ) { Assertions.assertThat( e.getLocation().compareTo( new URI( "http://localhost:" + port + "/repository/" + id + "/org/apache/archiva/archiva-test/1.0/archiva-test-1.0.jar" ) ) ).isEqualTo( 0 ); throw e; } finally { getManagedRepositoriesService().deleteManagedRepository( id, false ); } }
/** * Given (up to MAX_ITEMS_PER_LISTING_PAGE) story ids, * fetch the latest information about those stories in bulk and return a hashmap of (story id -> story) * * @param storyShortIds list of SHORT reddit story ids to fetch (can be max MAX_ITEMS_PER_LISTING_PAGE) size * @return map of story id -> story pairs, not guaranteed though to exist * @throws RedditClientException */ @Nonnull public Map<String, RedditStory> getStoriesById(@Nonnull final Set<String> storyShortIds) throws RedditClientException { Preconditions.checkArgument(storyShortIds.size() <= MAX_ITEMS_PER_LISTING_PAGE, "Cannot request more than " + MAX_ITEMS_PER_LISTING_PAGE + " stories by id at a given time"); Preconditions.checkArgument(storyShortIds.size() > 0, "Empty list of ids passed to getStoriesById"); final Set<String> storyLongIds = new HashSet<>(storyShortIds.size()); for (final String storyId : storyShortIds) storyLongIds.add(RedditKind.STORY.getKey() + "_" + storyId); final RedditListing<RedditStory> stories; try { //fetch listing of all stories final JsonNode jsonNodeResponse = redditEndpoint.path("/by_id/" + Joiner.on(",").join(storyLongIds) + ".json") .queryParam("limit", MAX_ITEMS_PER_LISTING_PAGE) .request(MediaType.APPLICATION_JSON) .get(JsonNode.class); stories = new RedditListing<>(jsonNodeResponse, RedditStory.class); } catch (@Nonnull RedirectionException | ProcessingException | ClientErrorException | JsonProcessingException e) { this.clientExceptionMeter.mark(); throw new RedditClientException(e); } //convert to a map final Map<String, RedditStory> storyMap = new LinkedHashMap<>(storyShortIds.size()); for (final RedditStory story : stories) { storyMap.put(story.getId(), story); } return storyMap; }
/** * Gets the details about a specific subreddit * * @param subredditName the subreddit name (eg 'news' or 'gaybros') * @return RedditSubreddit object * @throws RedditClientException */ public RedditSubreddit getSubredditByName(@Nonnull final String subredditName) throws RedditClientException { try { final JsonNode jsonNodeResponse = redditEndpoint.path("/r/" + subredditName + "/about.json") .request(MediaType.APPLICATION_JSON) .get(JsonNode.class); return RedditThing.parseThing(jsonNodeResponse, RedditSubreddit.class); } catch (@Nonnull RedirectionException | ProcessingException | ClientErrorException e) { this.clientExceptionMeter.mark(); throw new RedditClientException(e); } }
@Test public void autnum_redirect() throws Exception { try { RestTest.target(getPort(), String.format("rdap/%s", "autnum/100")) .request(MediaType.APPLICATION_JSON_TYPE) .get(String.class); fail(); } catch (final RedirectionException e) { assertThat(e.getResponse().getHeaders().getFirst("Location").toString(), is("https://rdap.one.net/autnum/100")); } }
@Test public void inetnum_exact_match_redirect() { try { RestTest.target(getPort(), String.format("rdap/%s", "ip/193.0.0.0/21")) .request(MediaType.APPLICATION_JSON_TYPE) .get(String.class); fail(); } catch (final RedirectionException e) { assertThat(e.getResponse().getHeaders().getFirst("Location").toString(), is("https://rdap.one.net/ip/193.0.0.0/21")); } }
@Test public void inetnum_child_redirect() { try { RestTest.target(getPort(), String.format("rdap/%s", "ip/193.0.0.1")) .request(MediaType.APPLICATION_JSON_TYPE) .get(String.class); fail(); } catch (final RedirectionException e) { assertThat(e.getResponse().getHeaders().getFirst("Location").toString(), is("https://rdap.one.net/ip/193.0.0.1")); } }
@Test public void inet6num_exact_match_redirect() { try { RestTest.target(getPort(), String.format("rdap/%s", "ip/2001:67c:370::/48")) .request(MediaType.APPLICATION_JSON_TYPE) .get(String.class); fail(); } catch (final RedirectionException e) { assertThat(e.getResponse().getHeaders().getFirst("Location").toString(), is("https://rdap.one.net/ip/2001:67c:370::/48")); } }
@Test public void inet6num_child_redirect() { try { RestTest.target(getPort(), String.format("rdap/%s", "ip/2001:67c:370::1234")) .request(MediaType.APPLICATION_JSON_TYPE) .get(String.class); fail(); } catch (final RedirectionException e) { assertThat(e.getResponse().getHeaders().getFirst("Location").toString(), is("https://rdap.one.net/ip/2001:67c:370::1234")); } }
/** * Get a story listing for a given set of subreddits * * @param subreddits the subreddits to look at * @param sort the sort style * @param timeRange the time range to filter on * @param limit the max number of stories * @return set of reddit stories found in the search * @throws RedditClientException */ @Nonnull public Set<RedditStory> getStoryListingForSubreddits(@Nonnull final Set<String> subreddits, @Nonnull final SortStyle sort, @Nonnull final TimeRange timeRange, final int limit) throws RedditClientException { final Set<RedditStory> stories = new LinkedHashSet<>(); String currentAfter = ""; int lastCount = 0; while (stories.size() < limit) { final RedditListing<RedditStory> subListing; try { final JsonNode jsonNodeResponse = redditEndpoint.path("/r/" + Joiner.on("+").join(subreddits) + "/" + sort.toString() + ".json") .queryParam("limit", MAX_ITEMS_PER_LISTING_PAGE) .queryParam("after", currentAfter) .queryParam("t", timeRange.toString()) .request(MediaType.APPLICATION_JSON) .get(JsonNode.class); subListing = new RedditListing<>(jsonNodeResponse, RedditStory.class); } catch (@Nonnull RedirectionException | ProcessingException | ClientErrorException | JsonProcessingException e) { this.clientExceptionMeter.mark(); throw new RedditClientException(e); } if (subListing.getChildren().size() == 0) { break; //no more listing! } for (int i = 0; i < subListing.getChildren().size() && stories.size() < limit; i++) { stories.add(subListing.getChildren().get(i)); } if (stories.size() == lastCount) { break; //no more stories added, fail early } lastCount = stories.size(); currentAfter = subListing.getAfter(); } return stories; }