META-INF.resources.crosser.CrossDomain.jsp Maven / Gradle / Ivy
<%@ page language="java" pageEncoding="utf-8"%>
<%@page import="com.mycomm.YesHttp.core.HttpMethod"%>
<%@page import="com.mycomm.YesHttp.core.JsonRequest"%>
<%@page import="com.mycomm.YesHttp.core.Request"%>
<%@page import="com.mycomm.YesHttp.core.Response"%>
<%@page import="com.mycomm.YesHttp.core.StringRequest"%>
<%@page import="com.mycomm.YesHttp.core.TextBaseResponseListener"%>
<%@page import="com.mycomm.YesHttp.core.YesHttpEngine"%>
<%@page import="com.mycomm.YesHttp.core.YesHttpError"%>
<%@page import="com.mycomm.itool.SystemUtil"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.util.Map"%>
<%@page import="org.json.JSONObject"%>
<%
//TODO
final Enumeration p_names = request.getParameterNames();
final Enumeration h_headers = request.getHeaderNames();
String method = request.getParameter("__method__");
String reqUrl = request.getParameter("__url__");
final StringBuilder answer = new StringBuilder();
boolean jsonReq = "true".equalsIgnoreCase(request.getParameter("__jsonReq__"));
HttpMethod Method = "post".equalsIgnoreCase(method) ? HttpMethod.POST : HttpMethod.GET;
if (SystemUtil.isTxtEmpty(reqUrl)) {
answer.append("{\"success\":\"false\",\"errorMsg\":\"URL is null!\"}");
return;
}
if (HttpMethod.POST.equals(Method) && p_names == null) {
answer.append("{\"success\":\"false\",\"errorMsg\":\"NULL parameters in POST request!\"}");
return;
}
Request yesReq = null;
if (jsonReq) {
yesReq = new JsonRequest(reqUrl, new TextBaseResponseListener() {
@Override
public void responseMe(String msg) {
answer.append(msg);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(YesHttpError error) {
answer.append(error.getMessage());
}
}, null, Request.Protocol.HTTPS_IGNORE_CERT) {
@Override
public void getHeaders(Map hdrs) {
if (h_headers == null) {
return;
}
while (h_headers.hasMoreElements()) {
String key_hd = h_headers.nextElement() + "";
hdrs.put(key_hd, request.getHeader(key_hd));
}
}
JSONObject data = new JSONObject();
@Override
public String JsonBodyBuilder() {
if (!data.isEmpty()) {
return data.toString();
}
if (p_names == null) {
return "{}";
}
while (p_names.hasMoreElements()) {
String key_pn = p_names.nextElement() + "";
data.put(key_pn, request.getParameter(key_pn));
}
return data.toString();
}
};
} else {
yesReq = new StringRequest(Method, reqUrl, new TextBaseResponseListener() {
@Override
public void responseMe(String msg) {
answer.append(msg);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(YesHttpError error) {
answer.append(error.getMessage());
}
}, null, Request.Protocol.HTTPS_IGNORE_CERT) {
@Override
public void getHeaders(Map hdrs) {
if (h_headers == null) {
return;
}
while (h_headers.hasMoreElements()) {
String key_hd = h_headers.nextElement() + "";
hdrs.put(key_hd, request.getHeader(key_hd));
}
}
@Override
public void getParams(Map requestData) {
if (p_names == null) {
return;
}
while (p_names.hasMoreElements()) {
String key_pn = p_names.nextElement() + "";
requestData.put(key_pn, request.getParameter(key_pn));
}
}
};
}
if (yesReq != null) {
YesHttpEngine.getYesHttpEngine().send(yesReq);
}
out.println(answer.toString());
out.flush();
%>