サーブレットのゲットだけ対応した
簡単なコードです。
即席なのでスパゲッティーですが、
参考にしたい方はどうぞ。
(環境 jdk1.5+Tomcat5.5.x)
private static String TEST_URL = "http://www.dimage.co.jp/";
private static String TEST_HOST = "www.dimage.co.jp";
private static String LOCALHOST_URL = "localhost";
private static String LOCALHOST_URL_8080 = "localhost:8080/Test/wa";
private static String LOCALHOST_FULL_URL = "http://localhost:8080/Test/wa/";
/**
* Get
*
*/
protected void doGet(HttpServletRequest arg0,
HttpServletResponse arg1) throws ServletException, IOException {
try {
//URLリプレイス
//http://localhost:8080/WaProxyTest/wa/
// → http://www.dimage.co.jp/ に置換
String connectUrl = arg0.getRequestURL().toString().replaceAll(LOCALHOST_FULL_URL, TEST_URL);
//URLオブジェクト作成
URL url = new URL(connectUrl);
//URLコネクト作成
URLConnection uc = url.openConnection();
//タイムアウト設定
uc.setReadTimeout(3000);//3秒
uc.setConnectTimeout(1000);//1秒
//リクエストヘッダー情報をすべて取得して、
//接続先のリクエストヘッダーに当て込む
//その際、403を回避するためキャッシュ関連情報
//if-modified-since、if-none-match、ETag
//を取り除く
//すなわち403は放置
System.out.println("INPUT HEADER---");
Enumeration headers = arg0.getHeaderNames();
ArrayList headerList = new ArrayList();
for (Enumeration e = headers ; e.hasMoreElements() ;) {
String key = (String)e.nextElement();
String value = arg0.getHeader(key);
// ヘッダー内のFQDNを書き換える。
if(value != null && value.indexOf(LOCALHOST_URL)!=-1){
value = TEST_HOST;
}
// ログ
System.out.println(key+":"+value);
if(key != null){
if("if-modified-since".equals(key)){
continue;
}
if("if-none-match".equals(key)){
continue;
}
if("ETag".equals(key)){
continue;
}
}
//接続先ヘッダーへ格納
uc.setRequestProperty(key,arg0.getHeader(key));
}
System.out.println("---");
//接続先から取得
InputStream reqStream = uc.getInputStream();
boolean typeImage = false;
System.out.println("OUTPUT HEADER---");
//接続先から取得したレスポンスヘッダーを
//ブラウザへ返すサーブレットレスポンスヘッダーへ格納
//キャッシュ関連はカット
for(int i=0; true ; i++) { // ヘッダがあるかぎり
String value = uc.getHeaderField(i);
if(value==null) break; //
String key = uc.getHeaderFieldKey(i);
System.out.println(key+":"+value);
if("Content-Type".equals(key)){
if(value != null &&value.indexOf("image")!=-1){
typeImage = true;
}
}
if("if-modified-since".equals(key)){
continue;
}
if("if-none-match".equals(key)){
continue;
}
if("ETag".equals(key)){
continue;
}
//チャンクはhtmlを取得してる時点で分解されているので
//ヘッダーからはカットする
//(めんどい)
if("Transfer-Encoding".equals(key)){
continue;
}
if(i!=0){
arg1.setHeader(key, value);
}
}
System.out.println("---");
//画像か、もしくは違うデータで処理系を分ける。
//pdfとかは上記のContent-Typeで判別するればおk
if(typeImage){
ServletOutputStream sOutStream = arg1.getOutputStream();
int out;
while((out=reqStream.read())!=-1){sOutStream.write(out);}
sOutStream.close();
reqStream.close();
}else{
BufferedReader bufReader = new BufferedReader(new InputStreamReader(reqStream,CODE_TYPE_UTF_8));
//レスポンスへ返送
ServletOutputStream sOutStream = arg1.getOutputStream();
String line;
while ((line = bufReader.readLine()) != null) {
//HTMLのURLを変換(たんなるリプレイス)作りこみ必要
line = line.replaceAll(TEST_HOST, LOCALHOST_URL_8080);
sOutStream.println(line);
}
reqStream.close();
sOutStream.close();
}
} catch (Exception e) {
log.error(e);
System.out.println(e);
}
}