Skip to main content
summaryrefslogtreecommitdiffstats
blob: 13f50fa802225ef99a9d80517dde4db20a5847da (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/*******************************************************************************
 * Copyright (c) 2007 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * IBM Corporation - initial API and implementation
 * yyyymmdd bug      Email and other contact information
 * -------- -------- -----------------------------------------------------------
 * 20070124   167487 gilberta@ca.ibm.com - Gilbert Andrews
 *******************************************************************************/
package org.eclipse.wst.ws.internal.common;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class HTTPUtility {

	public String handleRedirect(String urlString){
		URLConnection conn = null;
		String urlRedirect = urlString;
		int time = 0;
		while(urlRedirect!= null){
			if (time == 6) return urlRedirect;
			try{
				URL url = new URL(urlRedirect);
				conn = url.openConnection();
			}catch(Exception exc){
				return urlRedirect;
			}
			if (conn instanceof HttpURLConnection)
				{
				HttpURLConnection http = (HttpURLConnection) conn;
				http.setInstanceFollowRedirects(false);
	         
				try{
					int code = http.getResponseCode();
					if (code >= 300 && code <= 307 && code != 306 &&
	        			 code != HttpURLConnection.HTTP_NOT_MODIFIED)
					{
						String urlRedirect2 = changeSlash(http.getHeaderField("Location"));
	           	 		if (urlRedirect2 == null || urlRedirect.equals(urlRedirect2)) return urlRedirect;
	          	 		else urlRedirect = urlRedirect2;
					}else return urlRedirect;
				
				
				}catch(IOException exc){
					return urlRedirect; 
				}
			}
			else return urlRedirect;
			time++;
		}
		return urlString;
	}

	public String changeSlash(String url){
		String temp = "";
		try{
			temp = url.replace('\\', '/');
		}catch(Exception exc){
			return url;
		}
		return temp;
	}
}

Back to the top