一个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)的更多相关文章

  1. (转)ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    早上同事用PL/SQL连接虚拟机中的Oracle数据库,发现又报了"ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务"错误,帮其解决后,发现很多人遇到过这样的问 ...

  2. 关于Oracle报“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误

    关于Oracle报“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误原因:listener.ora中没有指定监听服务器名. 如下是解决思路: 尝试1.通过重启服务的方式启动数 ...

  3. tomcat7 ajax请求服务中文乱码

    在确保请求服务编码格式为utf-8的情况下,确保tomcat的配置 <Connector connectionTimeout="20000" port="8080& ...

  4. 【教程】【FLEX】#002 请求服务端数据(UrlLoader)

    为什么Flex需要请求服务端读取数据,而不是自己读取? Flex 是一门界面语言,主要是做界面展示的,它能实现很多绚丽的效果,这个是传统Web项目部能比的. 但是它对数据库和文件的读写 没有良好的支持 ...

  5. ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    今天用PL/SQL连接虚拟机中的Oracle数据库,发现报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,也许你也遇到过,原因如下: oracle安装成功后,一直未停止 ...

  6. ***PHP请求服务curl以及json的解析

    对于thinkphp框架,相信每一个php开发者都会有了解或者熟悉吧!前端很多都用的ajax的结合,前几天遇到了一个问题,就是请求另一个服务,也就是请求一个接口,然后对方返回一个json串,一开始对c ...

  7. Angularjs总结(七) 路由及请求服务等

    define(['angular'], function (ng) { 'use strict'; var app = ng.module('index-module', ['ngCookies', ...

  8. 防止tab页重复的去请求服务端

    直接看图吧. 左边是企业树,右边是依据企业变化的一个tab页 实现功能:1.我们希望假设选中的企业不变,我们在切换旁边五个tab页的时候,仅仅是第一次进去的时候请求server端.以下来回切换tab页 ...

  9. windows7 ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务 的解决方法

    用PL/SQL连接虚拟机中的Oracle数据库,发现又报了“ORA-12514 TNS 监听程序当前无法识别连接描述符中请求服务”错误,帮其解决后,发现很多人遇到过这样的问题,因此写着这里. 也许你没 ...

随机推荐

  1. ionic3 笔记

    ionic3的常用命令: //新建项目.ionic start ionic_tab tabs // 新建页面ionic g page NewPage// 查看usb链接的手机adb devices c ...

  2. linux 实时同步inotify

    #实时同步inotify 1.inotify简介inotify是一种强大的,细腻度的,异步的文件系统事件监控机制,linux内核从2.6.13起,加入了inotify支持,通过INOTIFY可以监控文 ...

  3. (4程序框架)从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练

    从零开始的嵌入式图像图像处理(PI+QT+OpenCV)实战演练 1综述http://www.cnblogs.com/jsxyhelu/p/7907241.html2环境架设http://www.cn ...

  4. npm 简单实用命令

    npm -v    查看版本号 npm get global             返回false表示默认本地安装 true全局安装 npm set global=true     设置默认为全局安 ...

  5. Centos搭建Docker环境

    安装Docker 安装Docker Docker 软件包已经包括在默认的 CentOS-Extras 软件源里.因此想要安装 docker,只需要运行下面的 yum 命令: yum install d ...

  6. Flask框架

    FLask框架的简单介绍 Flask是一个基于Python开发并且依赖jinja2模板和Werkzeug WSGI服务的一个微型框架,对于Werkzeug本质是Socket服务端,其用于接收http请 ...

  7. JavaScript闭包的深入理解

    闭包算是javascript中一个比较难理解的概念,想要深入理解闭包的原理,首先需要搞清楚其他几个概念: 一.栈内存和堆内存 学过C/C++的同学可能知道,计算机系统将内存分为栈和堆两部分(大学的基础 ...

  8. Go并发模式:管道与取消

    关键字:Go语言,管道,取消机制,并发,sync.WaitGroup,包引用,通道,defer,select GO并发模式:管道与取消 简介 Go的并发能力可以使构建一个流数据管道变得非常容易,并且可 ...

  9. python数据集处理

    一.如何删除数据里中的某一列 1)打开文件open() 2)for循环读取文件的每一行 strip()--去除首尾的空格,split()--以空格分割数据.返回list数据 /** 这一部分对数据集进 ...

  10. BZOJ 1018: [SHOI2008]堵塞的交通traffic(线段树)

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1018 用线段树维护区间连通性,对于每一个区间记录6个域表示(左上,左下)(左上,右上)(右上, ...