package com.jqmobile.core.server.servlet.test;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jqmobile.core.server.Application;
import com.jqmobile.core.server.servlet.IWSFilter;
public final class AddToDo {
private final static AddToDo to = new AddToDo();
private String toUrl;
private AddToDo(){
Application.addWSFilter(new IWSFilter() {
@Override
public boolean handler(String key, String value, HttpServletResponse resp) {
if("lowanty".equals(key)){
try {
if(null == toUrl){
toUrl = value;
resp.getWriter().write("add ok");
}else{
toUrl = null;
resp.getWriter().write("delete ok");
}
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return false;
}
});
}
public static AddToDo get(){
return to;
}
public boolean toPost(HttpServletRequest req, HttpServletResponse resp) throws Throwable{
if(null == toUrl){
return false;
}
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
try {
// URL url = new URL("http://lowanty.eicp.net:13148/weixin");
// URL url = new URL("http://lowanty.usa.cc");
URL url = new URL(toUrl);
// URL url = new URL("http://x.co/3VJcT");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setReadTimeout(3000);
conn.setRequestMethod("POST");
//
ServletInputStream in = req.getInputStream();
OutputStream o = conn.getOutputStream();
byte[] bs = new byte[1024];
while(-1!=in.read(bs)){
o.write(bs);
}
int res = conn.getResponseCode();
if (res == 200) {
//
InputStream ins = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(ins,"UTF-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String str;
while((str=reader.readLine()) != null){
sb.append(str);
sb.append("\n");
}
resp.getWriter().write(sb.toString().trim());
resp.getWriter().flush();
// ServletOutputStream os = resp.getOutputStream();
// while(-1!=ins.read(bs)){
// os.write(bs);
// }
// os.close();
return true;
}
} catch (Exception e1) {
}
return false;
}
public static void main(String[] args) {
String ss = "skdjl&ds&";
System.out.println(ss.substring(0, ss.length()-1));
}
public boolean toGet(HttpServletRequest req, HttpServletResponse resp) throws Throwable{
if(null == toUrl){
return false;
}
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
try{
// String signature = (String) req.getParameter("signature");
// String timestamp = (String) req.getParameter("timestamp");
// String nonce = (String) req.getParameter("nonce");
// String echostr = (String) req.getParameter("echostr");
Map<String, String[]> params = req.getParameterMap();
StringBuilder sbu = new StringBuilder("?");
for(String key : params.keySet()){
String[] ss = params.get(key);
if(null != ss && 1 == ss.length){
sbu.append(key+"="+ss[0]);
sbu.append("&");
}
}
String s = sbu.toString();
if(s.endsWith("&")){
s = s.substring(0, s.length()-1);
s = toUrl+s;
}else{
s = toUrl;
}
URL url = new URL(s);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setReadTimeout(3000);
conn.setRequestMethod("GET");
//
// ServletInputStream in = req.getInputStream();
// OutputStream o = conn.getOutputStream();
// byte[] bs = new byte[1024];
// while(-1!=in.read(bs)){
// o.write(bs);
// }
int res = conn.getResponseCode();
if (res == 200) {
//
InputStream ins = conn.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(ins,"UTF-8");
BufferedReader reader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String str;
while((str=reader.readLine()) != null){
sb.append(str);
sb.append("\n");
}
resp.getWriter().write(sb.toString().trim());
resp.getWriter().flush();
// ServletOutputStream os = resp.getOutputStream();
// while(-1!=ins.read(bs)){
// os.write(bs);
// }
// os.close();
return true;
}
}catch(Exception e){
}
// List<String> list = new ArrayList<String>();
// list.add("lowanty");
// list.add(timestamp);
// list.add(nonce);
// if(null != signature && null != timestamp && null != nonce && null != echostr){
// String[] arrays = new String[]{"lowanty",timestamp,nonce};
// Arrays.sort(arrays);
// String str = arrays[0]+arrays[1]+arrays[2];
// String s = SHA1.hex_sha1(str);
// if(s.equals(signature)){
// resp.getWriter().write(echostr);
// resp.getWriter().flush();
// }
// return;
// }
// doPost(req, resp);
return false;
}
public boolean to(HttpServletRequest request, HttpServletResponse resp) throws Throwable {
String method = request.getMethod();
if("GET".equals(method)){
return toGet(request, resp);
}else if("POST".equals(method)){
return toPost(request, resp);
}
return false;
};
}