134 lines
3.8 KiB
Plaintext
134 lines
3.8 KiB
Plaintext
<%@page session="false"%>
|
|
<%@page import="java.util.Set"%>
|
|
<%@page import="java.util.Map"%>
|
|
<%@page import="java.util.HashMap"%>
|
|
<%@page import="java.util.Iterator"%>
|
|
<%@page import="java.net.*,java.io.*"%>
|
|
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
|
|
<%
|
|
try {
|
|
|
|
// 한글인코딩
|
|
request.setCharacterEncoding("UTF-8");
|
|
|
|
//GET방식에서 key, value를 url에 넣는 과정
|
|
Map paramMap = request.getParameterMap();
|
|
Iterator iterator = paramMap.keySet().iterator();
|
|
|
|
// post, get에서 쓸 url을 각각 지정
|
|
String reqUrl = ((String[])paramMap.get("url"))[0];
|
|
|
|
if(reqUrl.indexOf("?") == -1){
|
|
reqUrl = reqUrl + "?";
|
|
} else {
|
|
if (reqUrl.charAt(reqUrl.length()-1) != '?') {
|
|
reqUrl += "&";
|
|
}
|
|
}
|
|
|
|
//get에서 knv를 url에 담는 과정
|
|
String middleUrl = "";
|
|
while (iterator.hasNext()) {
|
|
|
|
String key = (String) iterator.next();
|
|
|
|
if (key.equalsIgnoreCase("url")) {
|
|
continue;
|
|
}
|
|
|
|
String values[] = (String[]) paramMap.get(key);
|
|
|
|
if (values[0] != null) {
|
|
middleUrl = middleUrl + key.toUpperCase() + "=" + URLEncoder.encode(values[0], "UTF-8") + "&";
|
|
}
|
|
|
|
// System.out.println("key : " + key + " ___ value : " + values[0]);
|
|
|
|
}
|
|
|
|
String resultUrl = reqUrl + middleUrl;
|
|
|
|
URL url = null;
|
|
HttpURLConnection con = null;
|
|
|
|
if (request.getMethod().equalsIgnoreCase("GET")) {
|
|
|
|
url = new URL(resultUrl);
|
|
//System.out.println("request : (" + request.getMethod() + ") "+ resultUrl);
|
|
|
|
con = (HttpURLConnection) url.openConnection();
|
|
con.setDoOutput(true);
|
|
con.setRequestMethod(request.getMethod());
|
|
int clength = request.getContentLength();
|
|
if (clength > 0) {
|
|
con.setDoInput(true);
|
|
InputStream istream = request.getInputStream();
|
|
OutputStream os = con.getOutputStream();
|
|
final int length = 5000;
|
|
byte[] bytes = new byte[length];
|
|
int bytesRead = 0;
|
|
while ((bytesRead = istream.read(bytes, 0, length)) > 0) {
|
|
os.write(bytes, 0, bytesRead);
|
|
}
|
|
}
|
|
|
|
} else {
|
|
|
|
url = new URL(reqUrl);
|
|
//System.out.println("request : (" + request.getMethod() + ") "+ reqUrl);
|
|
|
|
// POST방식에서 inputStream을 StringBuilder에 붙임
|
|
StringBuilder sb = new StringBuilder();
|
|
InputStreamReader is = new InputStreamReader(request.getInputStream());
|
|
BufferedReader br = new BufferedReader(is);
|
|
String read = br.readLine();
|
|
|
|
while (read != null) {
|
|
sb.append(read);
|
|
read = br.readLine();
|
|
}
|
|
|
|
String strBody = sb.toString();
|
|
System.out.println("before body : " + strBody);
|
|
|
|
con = (HttpURLConnection) url.openConnection();
|
|
con.setRequestMethod(request.getMethod());
|
|
con.setRequestProperty("charset", "UTF-8");
|
|
con.setDoOutput(true);
|
|
con.setUseCaches(false);
|
|
|
|
if (strBody.trim().startsWith("<")) {
|
|
// XML of Post Method
|
|
con.setRequestProperty("Content-Type", "application/xml");
|
|
} else {
|
|
// KVP of Post Method
|
|
strBody = strBody + middleUrl;
|
|
con.setRequestProperty("Content-Type", request.getContentType());
|
|
}
|
|
|
|
System.out.println("after body : " + strBody);
|
|
// StringBuilder를 byte화 시켜서 post로 넘어온 데이터를 담는다
|
|
byte[] postData = strBody.getBytes();
|
|
con.setRequestProperty("Content-Length", Integer.toString(postData.length));
|
|
con.getOutputStream().write(postData);
|
|
|
|
}
|
|
|
|
out.clear();
|
|
out = pageContext.pushBody();
|
|
OutputStream ostream = response.getOutputStream();
|
|
response.setContentType(con.getContentType());
|
|
|
|
InputStream in = con.getInputStream();
|
|
final int length = 5000;
|
|
byte[] bytes = new byte[length];
|
|
int bytesRead = 0;
|
|
while ((bytesRead = in.read(bytes, 0, length)) > 0) {
|
|
ostream.write(bytes, 0, bytesRead);
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
System.out.println("proxy exception : " + e);
|
|
response.setStatus(500);
|
|
}
|
|
%> |