2 * Copyright 2000-2011 JetBrains s.r.o.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
17 package jetbrains.buildServer.nuget.server.feed.reader;
\r
19 import com.intellij.openapi.util.Pair;
\r
20 import org.apache.http.Header;
\r
21 import org.apache.http.HttpResponse;
\r
22 import org.apache.http.HttpStatus;
\r
23 import org.apache.http.client.methods.HttpGet;
\r
24 import org.apache.http.client.params.ClientPNames;
\r
25 import org.jetbrains.annotations.NotNull;
\r
27 import java.io.IOException;
\r
30 * Created by Eugene Petrenko (eugene.petrenko@gmail.com)
\r
31 * Date: 12.08.11 10:24
\r
33 public class UrlResolver {
\r
34 private final FeedClient myClient;
\r
35 private final FeedGetMethodFactory myMethods;
\r
37 public UrlResolver(@NotNull final FeedClient client,
\r
38 @NotNull final FeedGetMethodFactory methods) {
\r
40 myMethods = methods;
\r
44 * Generates GET request to a given URL.
\r
45 * If HTTP Status 3xx is returned, Location header is
\r
46 * used to generate next request unless non 3xx status
\r
48 * @param feedUrl url to ping and resolve
\r
49 * @return resolved URL and HttpResponse
\r
50 * @throws IOException if failed to communicate or non 3xx or 200 status returned
\r
53 public Pair<String, HttpResponse> resolvePath(@NotNull String feedUrl) throws IOException {
\r
54 for(int _ = 100; _-->0;) {
\r
55 HttpGet ping = myMethods.createGet(feedUrl);
\r
56 ping.getParams().setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, false);
\r
58 final HttpResponse execute = myClient.getClient().execute(ping);
\r
59 final int statusCode = execute.getStatusLine().getStatusCode();
\r
60 if (statusCode / 100 == 3) {
\r
61 final Header location = execute.getFirstHeader("Location");
\r
62 if (location != null) {
\r
63 feedUrl = location.getValue();
\r
68 if (statusCode != HttpStatus.SC_OK) {
\r
69 throw new IOException("Failed to connect to " + feedUrl);
\r
71 return Pair.create(feedUrl, execute);
\r
73 throw new IOException("Failed to resolve redirects");
\r