|
|
|
package com.zhonglai.socket.html.http.service.impl;
|
|
|
|
|
|
|
|
import com.google.gson.JsonObject;
|
|
|
|
import com.zhonglai.dto.Message;
|
|
|
|
import com.zhonglai.dto.MessageCode;
|
|
|
|
import com.zhonglai.socket.html.action.Base;
|
|
|
|
import com.zhonglai.socket.html.http.service.AnalysisService;
|
|
|
|
import com.zhonglai.socket.html.http.util.HttpResponseUtil;
|
|
|
|
import com.zhonglai.socket.html.http.util.ParameterAnalysisUtil;
|
|
|
|
import io.netty.channel.ChannelHandlerContext;
|
|
|
|
import io.netty.handler.codec.http.FullHttpRequest;
|
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
|
|
|
import java.lang.reflect.InvocationTargetException;
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 解析2个路径协议的业务
|
|
|
|
*/
|
|
|
|
public class Path2AnalysisServiceImpl implements AnalysisService {
|
|
|
|
String actionPath = "";
|
|
|
|
|
|
|
|
public Path2AnalysisServiceImpl( String actionPath )
|
|
|
|
{
|
|
|
|
this.actionPath = actionPath;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void analysis(ChannelHandlerContext ctx, FullHttpRequest request) {
|
|
|
|
String uri = request.uri();
|
|
|
|
String path = ParameterAnalysisUtil.UrlPage(uri);
|
|
|
|
if(StringUtils.isNoneBlank(path))
|
|
|
|
{
|
|
|
|
String[] paths = path.split("/");
|
|
|
|
if(paths.length==3)
|
|
|
|
{
|
|
|
|
String clasPath = actionPath + "."+paths[1].substring(0, 1).toUpperCase() + paths[1].substring(1)+"Action";
|
|
|
|
String name = paths[2];
|
|
|
|
System.out.println("解析到执行方法路径:"+clasPath+" 执行方法名称"+name);
|
|
|
|
|
|
|
|
JsonObject parameter = ParameterAnalysisUtil.URLRequestJson(uri); //获取url参数
|
|
|
|
if(null == parameter)
|
|
|
|
{
|
|
|
|
parameter = new JsonObject();
|
|
|
|
}
|
|
|
|
|
|
|
|
Set set = request.headers().names();
|
|
|
|
Iterator it = set.iterator();
|
|
|
|
while(it.hasNext()){//遍历 获取head参数
|
|
|
|
Object object = it.next();
|
|
|
|
if(object instanceof String)
|
|
|
|
{
|
|
|
|
String key = (String) object;
|
|
|
|
parameter.addProperty(key,request.headers().get(key)+"");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Class<?> cls = null; // 取得Class对象
|
|
|
|
try {
|
|
|
|
cls = Class.forName(clasPath);
|
|
|
|
Object obj = cls.newInstance(); //反射实例化对象
|
|
|
|
|
|
|
|
Base base = (Base) obj;
|
|
|
|
base.setParameter(parameter);//先设置参数
|
|
|
|
|
|
|
|
Method m = cls.getDeclaredMethod(name);
|
|
|
|
Message message = (Message) m.invoke(obj); //执行方法
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,message); //返回
|
|
|
|
return;
|
|
|
|
} catch (ClassNotFoundException e) {
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"没有找到这个方法"+clasPath));
|
|
|
|
return;
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"方法类实例失败"+clasPath));
|
|
|
|
return;
|
|
|
|
} catch (InstantiationException e) {
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"方法类实例失败"+clasPath));
|
|
|
|
return;
|
|
|
|
} catch (NoSuchMethodException e) {
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"方法获取失败"+clasPath+" "+name));
|
|
|
|
return;
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"方法执行失败"+clasPath+" "+name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpResponseUtil.sendOkMessage(ctx,new Message(MessageCode.DEFAULT_FAIL_CODE,"方法规则不符合协议规则"));
|
|
|
|
}
|
|
|
|
} |
...
|
...
|
|