package com.jqmobile.core.server.servlet;
import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jqmobile.core.http.HttpUtils;
import com.jqmobile.core.server.Application;
import com.jqmobile.core.server.servlet.load.UploadClient;
import com.jqmobile.core.server.servlet.test.AddToDo;
import com.jqmobile.core.server.session.Session;
import com.jqmobile.core.utils.json.JSONArray;
import com.jqmobile.core.utils.json.JSONException;
import com.jqmobile.core.utils.json.JSONObject;
import com.jqmobile.core.utils.plain.StringUtils;
public class CoreHttpServlet extends HttpServlet{
private static final long serialVersionUID = 5915188818600181050L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
handler(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
handler(req, resp);
}
void handler(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
try{
doAction(req,resp);
}finally{
Session.release();
}
}
private void doAction(HttpServletRequest request, HttpServletResponse resp) throws IOException {
//
request.setCharacterEncoding(HttpUtils.CHARSET);
resp.setCharacterEncoding(HttpUtils.CHARSET);
if(other(request,resp)){
return;
}
//
StringBuilder sResult = new StringBuilder();
InputStream httpInput = null;
BufferedReader httpReader = null;
// 读取数据
httpInput = request.getInputStream();
httpReader = new BufferedReader(new InputStreamReader(httpInput,
HttpUtils.CHARSET));
String line = null;
while ((line = httpReader.readLine()) != null) {
if (sResult.length() > 0) {
sResult.append("\n");
}
sResult.append(line);
}
try {
JSONObject jRequestData = new JSONObject(sResult.toString());
String intfName = jRequestData
.getString(HttpUtils.REQUEST_TAG_INTF_NAME);
String methodName = jRequestData
.getString(HttpUtils.REQUEST_TAG_METHOD_NAME);
JSONArray params =jRequestData.getJSONArray(HttpUtils.REQUEST_TAG_ARGS);
String session = null;
if(jRequestData.has(HttpUtils.REQUEST_SESSION)){
session = jRequestData.getString(HttpUtils.REQUEST_SESSION);
}
//
boolean isLogin = false;
if(null == session){
isLogin = true;
}
Session.activate(request);
JSONObject ret = HttpService.invoke(intfName,
methodName, params, isLogin);
if(null == ret){
return;
}
if(isLogin) ret.append(HttpUtils.RESPONSE_SESSION, Session.getSession().getId());
output(resp, ret);
} catch (Throwable e) {
JSONObject ret = new JSONObject();
try {
HttpService.returnException(ret, e);
} catch (JSONException e1) {
e1.printStackTrace();
}
output(resp, ret);
e.printStackTrace();
}
}
private boolean other(HttpServletRequest request, HttpServletResponse resp) {
String path = request.getRequestURI();
String action = null;
if(path.contains(".do")){
action = path.substring(path.lastIndexOf("/"), path.indexOf(".do"));
}else if(path.endsWith("upload.client")){
String session = request.getHeader(HttpUtils.REQUEST_SESSION);
request.setAttribute(HttpUtils.REQUEST_SESSION, session);
Session.activate(request);
String name = path.substring(path.lastIndexOf("/")+1, path.indexOf("upload.client"));
name = StringUtils.valueOfBytesStr(name);
try {
boolean b = UploadClient.openUpload().upload(name, request.getInputStream());
JSONObject ret = HttpService.autoReturnJson(b, List.class);
output(resp, ret);
} catch (IOException e) {
JSONObject ret = new JSONObject();
try {
HttpService.returnException(ret, e);
} catch (JSONException e1) {
e1.printStackTrace();
}
try {
output(resp, ret);
} catch (IOException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
return true;
}else if(path.endsWith("download.client")){
String session = request.getHeader(HttpUtils.REQUEST_SESSION);
request.setAttribute(HttpUtils.REQUEST_SESSION, session);
Session.activate(request);
String name = path.substring(path.lastIndexOf("/")+1, path.indexOf("download.client"));
name = StringUtils.valueOfBytesStr(name);
try {
InputStream in = UploadClient.openUpload().download(name);
ServletOutputStream os = resp.getOutputStream();
// byte[] bs = new byte[1024];
int b;
while ((b=in.read())!=-1) {
os.write(b);
}
os.close();
} catch (IOException e) {
e.printStackTrace();
}
return true;
}else if(path.contains(".client")){
action = path.substring(path.lastIndexOf("/"), path.indexOf(".client"));
}
if(null != action){
boolean ok = false;
if("/test".equals(action)){
String key = request.getParameter("key");
String value = request.getParameter("value");
for(IWSFilter f : Application.getFilters()){
if(f.handler(key, value, resp)) ok = true;
}
}else if("/to".equals(action)){
try {
String method = request.getMethod();
if("GET".equals(method)){
AddToDo.get().toGet(request, resp);
}else if("POST".equals(method)){
AddToDo.get().toPost(request, resp);
}else{
return AddToDo.get().to(request, resp);
}
} catch (Throwable e) {
return false;
}
// try{
// if(AddToDo.get().toGet(req,resp)) return;
// }catch(Throwable e){
// }
// try{
// if(AddToDo.get().to(req,resp)) return;
// }catch(Throwable e){
// }
}
return ok;
}
return false;
}
private void output(final HttpServletResponse response, JSONObject ret)
throws IOException {
String text = ret.toString();
response.setContentType(HttpUtils.HTTP_CONNTENT_TYPE + "; charset="
+ HttpUtils.CHARSET);
response.setCharacterEncoding(HttpUtils.CHARSET);
response.getOutputStream().write(
text.toString().getBytes(HttpUtils.CHARSET));
response.getOutputStream().flush();
}
//===================
@Override
protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
throw new ServletException("非法请求");
}
@Override
public void init() throws ServletException {
super.init();
Application app = Application.getApplication();
app.initSystem();
app.initService();
AddToDo.get();
}
}