com.mycomm.cross.utils.WebUtils Maven / Gradle / Ivy
/*
* Copyright 2018 jw362j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycomm.cross.utils;
import com.mycomm.YesHttp.core.HttpMethod;
import com.mycomm.YesHttp.core.JsonRequest;
import com.mycomm.YesHttp.core.Request;
import com.mycomm.YesHttp.core.Response;
import com.mycomm.YesHttp.core.StringRequest;
import com.mycomm.YesHttp.core.TextBaseResponseListener;
import com.mycomm.YesHttp.core.YesHttpEngine;
import com.mycomm.YesHttp.core.YesHttpError;
import com.mycomm.itool.SystemUtil;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.json.JSONObject;
/**
*
* @author jw362j
*/
public class WebUtils {
private void test(HttpServletRequest request) {
//TODO
final Enumeration p_names = request.getParameterNames();
final Enumeration h_headers = request.getHeaderNames();
String reqUrl = request.getParameter("url");
boolean jsonReq = "true".equalsIgnoreCase(request.getParameter("jsonReq"));
HttpMethod Method = "post".equalsIgnoreCase(request.getParameter("method")) ? HttpMethod.POST : HttpMethod.GET;
final StringBuilder answer = new StringBuilder();
if (SystemUtil.isTxtEmpty(reqUrl)) {
answer.append("{\"success\":\"false\",\"errorMsg\":\"URL is null!\"}");
}
if (HttpMethod.POST.equals(Method) && p_names == null) {
answer.append("{\"success\":\"false\",\"errorMsg\":\"NULL parameters in POST request!\"}");
return;
}
Request yesReq = null;
if (jsonReq) {
if (!SystemUtil.isTxtEmpty(reqUrl)) {
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 {
if (!SystemUtil.isTxtEmpty(reqUrl)) {
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);
}
}
}