请求服务(RequestService)
一个module中的web组件,负责将Service的结果按照适当的规范输出给前端。
格式:http://server/moduleID/param0/param1/paramN/p.TYPE
格式上包含三部分:
moduleID:将请求分发到具体的模块,分发器参考下面具体介绍;
paramX:参数部分
TYPE:使用什么请求服务完成,对应到module中的一个具体的RequestService
从MVC的角度来看,这个相当于C,将服务提供的模型数据以适当的形式展现给前端;
一、请求分发器
framework中的web组件,一个标准的Http Filter或HttpServlet,下面是Filter版本的代码
public void doFilter(ServletRequest sreq, ServletResponse sresp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest)sreq;
HttpServletResponse resp = (HttpServletResponse)sresp;
req.setAttribute(Constants.SERVLET_CONTEXT, servletContext);
//
String path = req.getRequestURI();
if((path == null || path.length() <= 1) && !StringUtils.isEmpty(welcomePage))
path = this.welcomePage;
//
LocalModule module = null;
String[] uris = null;
String reqType = null;
if(path.indexOf(".") > 0) {
path = path.replaceAll("/+", "/").substring(req.getContextPath().length());
reqType = path.substring(path.lastIndexOf(".") + 1);
final String pathEff = path.substring(0, path.lastIndexOf("."));
uris = StringUtils.split(pathEff, "/");
final String moduleId = uris[0];
module = Application.getInstance().getModules().exists(moduleId)?Application.getInstance().getModules().getLocalModule(moduleId): null;
Principal principal = (Principal)req.getSession().getAttribute(Principal.PRINCIPAL);
if(principal == null && req.getUserPrincipal() != null) {
principal = new SimpleUserPrincipal(req.getUserPrincipal().getName(), req.getUserPrincipal().getName(), req.getUserPrincipal());
}
if(module != null) {
req.setAttribute(Constants.MODULE, module);
Data request = DataUtils.convert(module, req);
ThreadContext ctx = new ThreadContext(module, request, principal);
ThreadContext.setContext(ctx);
}
}
//
if(module != null && !StringUtils.isEmpty(reqType) && module.canHandleRequest(reqType)) {
final String[] pathItems = new String[uris.length - 1];
System.arraycopy(uris, 1, pathItems, 0, uris.length - 1);
processRequestInChain(module, reqType, req, resp, pathItems);
} else {
chain.doFilter(sreq, sresp);
}
}
判断规则:
1、请求的模块存在:Application.getInstance().getModules().exists(moduleId)
2、请求的模块能够处理该类型请求:module.canHandleRequest(reqType)
二、开发RequestService
需要实现接口RequestService:
public interface RequestService {
public void service(LocalModule module, String[] pathItems, HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException;
}
一个典型的把服务的处理结果输出为Json的RequestService:
public class JsonRequestService extends AbstractRequestService implements RequestService {
private final static Logger logger = Logger.getLogger(JsonRequestService.class);
public void service(LocalModule module, String[] uris, HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Data request = DataUtils.convert(module, req);
request.put(Constants.ACTION_TYPE, Constants.ACTION_TYPE_ACTION);
resp.setContentType("application/json");
JSONResult result = new JSONResult();
result.setRequest(request);
String serviceId = uris[0];
if (!module.getModuleConfig().getServiceConfigs().containsKey(serviceId))
serviceId += "Action";
String method = uris[1];
String serviceModuleId = uris.length == 3 ? uris[2] : module.getId();
try {
final Data data = ServiceInvoker.invoke(module.getId(), serviceModuleId, serviceId + ":" + method, request);
String results = (String) data.get("result");
result.setResult(results);
result.setData(data);
} catch (ObjectNotFoundException e) {
resp.sendError(404);
return;
} catch (AppException e) {
logger.error(this.getClass().getName(), e);
result.setResult(e.getErrorCode());
result.setMessage(e.getMessage());
} catch (Exception e) {
logger.error(this.getClass().getName(), e);
result.setResult("1");
result.setMessage(e.getMessage());
}
String jsonString = result.toJSONString();
logger.debug(jsonString);
resp.getWriter().append(jsonString);
resp.flushBuffer();
}
}
三、注册
注册为服务,如下:
<service id="com.flyingwords.framework.request.impl.ShtmlRequestService" type="class" target="com.flyingwords.framework.request.impl.ShtmlRequestService">
<desc></desc>
<configs>
<config name="content-type">text/html; charset=UTF-8</config>
<config name="encoding">UTF-8</config>
</configs>
</service>
注册为RequestService,如下:
<requests>
<request type="json" service="com.flying.framework.request.impl.JsonRequestService"/>
</requests>
四、访问
http://server:port/pas/UserService/findByUserNameAndPassword.json
即可访问该接口了。
flying框架中,RequestService定义为服务的外延,是WEB访问服务的桥梁。
请求服务(RequestService)的更多相关文章
- (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...
- 关于Oracle报“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误
关于Oracle报“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误原因:listener.ora中没有指定监听服务器名. 如下是解决思路: 尝试1.通过重启服务的方式启动数 ...
- tomcat7 ajax请求服务中文乱码
在确保请求服务编码格式为utf-8的情况下,确保tomcat的配置 <Connector connectionTimeout="20000" port="8080& ...
- 【教程】【FLEX】#002 请求服务端数据(UrlLoader)
为什么Flex需要请求服务端读取数据,而不是自己读取? Flex 是一门界面语言,主要是做界面展示的,它能实现很多绚丽的效果,这个是传统Web项目部能比的. 但是它对数据库和文件的读写 没有良好的支持 ...
- ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
今天用PL/SQL连接虚拟机中的Oracle数据库,发现报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,也许你也遇到过,原因如下: oracle安装成功后,一直未停止 ...
- ***PHP请求服务curl以及json的解析
对于thinkphp框架,相信每一个php开发者都会有了解或者熟悉吧!前端很多都用的ajax的结合,前几天遇到了一个问题,就是请求另一个服务,也就是请求一个接口,然后对方返回一个json串,一开始对c ...
- Angularjs总结(七) 路由及请求服务等
define(['angular'], function (ng) { 'use strict'; var app = ng.module('index-module', ['ngCookies', ...
- 防止tab页重复的去请求服务端
直接看图吧. 左边是企业树,右边是依据企业变化的一个tab页 实现功能:1.我们希望假设选中的企业不变,我们在切换旁边五个tab页的时候,仅仅是第一次进去的时候请求server端.以下来回切换tab页 ...
- windows7 ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法
用PL/SQL连接虚拟机中的Oracle数据库,发现又报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,帮其解决后,发现很多人遇到过这样的问题,因此写着这里. 也许你没 ...
随机推荐
- 表单验证插件--formvalidation
表单验证是一个非常基础的功能,当你的表单项少的时候,可以自己写验证,但是当你的表单有很多的时候,就需要一些验证的插件.今天介绍一款很好用的表单验证插件,formvalidation.其前身叫做boot ...
- dom4j 间隔插入节点 处理复杂的xml文档
前几周跟着老师做了一个小项目,个人主要负责xml文档处理,处理过程还是比较复杂的.此外这篇文章并不是讲基本的dom4j读写xml文档, 所以阅读此文的前提是你已经有了dom4j或jdom等处理xml文 ...
- Python 抽象篇:面向对象之高阶用法
1.检查继承 如果想要查看一个类是否是另一个类的子类,可以使用内建的issubclass函数 如果想知道已知类的基类,可以直接使用特殊特性__bases__ 同时,使用isinstance方法检查一个 ...
- Swagger入门教程
[译]5.41 Swagger tutorial 单击此处查看原文 更多概念参见:Implementing Swagger with your API docs 关于 Swagger Swagger能 ...
- IdentityServer Topics(1)- 启动说明
启动 IdentityServer的启动是中间件和服务的组合来实现的. 所有配置都在你的启动类(Startup.cs)中完成. 配置服务 通过以下方式调用将IdentityServer服务添加到DI系 ...
- Nginx配置文件(2)
一.配置文件结构 1.全局块:配置影响nginx全局的指令.一般有运行nginx服务器的用户组,nginx进程pid存放路径,日志存放路径,配置文件引入,允许生成worker process数等. 2 ...
- 【转载】wifi一键配网smartconfig原理及应用
物联网给我们又提供了一种窃取WiFi密码的好方式:让智能设备主动断线. 同时也提供一种让智能设备连接到恶意WiFi的方式:设备一键配置功能时疯狂广播恶意WiFi的信息. 转自:http://blog. ...
- 一步一步从原理跟我学邮件收取及发送 2.邮箱的登录和绕不开的base64
一步一步从原理跟我学邮件收取及发送 2.邮箱的登录和绕不开的base64 好了,经过本系列上一篇文章 "1.网络命令的发送",假设大家已经掌握了 email 电子邮件的命令发送的方 ...
- HDU-5157Harry and magic string
题目:http://acm.hdu.edu.cn/showproblem.php?pid=5157 先从后往前插点,在构造回文树时,让cnt[i]+=cnt[fail[i]],然后维护一个后缀和a. ...
- HTML5 Canvas 数据持久化存储之属性列表
属性列表想必大家都不会陌生,正常用 HTML5 来做的属性列表大概就是用下拉菜单之类的,而且很多情况下,下拉列表还不够好看,怎么办?我试着用 HT for Web 来实现属性栏点击按钮弹出多功能选框, ...