ofbiz 服务引擎(一) controller中服务的调用解析

来源:互联网 发布:国外数据库网站 编辑:程序博客网 时间:2024/04/30 04:57

首先根据handler-controller.xml文件中对应handler文件,然后运行RequestHandler中的runEvent方法,方法如下:


/**     * @Description: 查找event处理句柄,并调用。通过request-map节点的event子节点的type属性值     * 从handler节点的解析map(type非view)中查找类,即handler节点的class属性值,     * 实例化、初始化后,其将调用其中处理event子节点中invoke属性值对应的方法。     * @param request 请求对象     * @param response 应答对象     * @param event event对象     * @param requestMap request-map节点对应的解析对象     * @param trigger 调用的类别,如event、view     * @throws EventHandlerException     * @return: String     */    public String runEvent(HttpServletRequest request, HttpServletResponse response,            ConfigXMLReader.Event event, ConfigXMLReader.RequestMap requestMap, String trigger) throws EventHandlerException {        //根据request-map节点的子节点event的type属性值,从eventFactory中获取相应eventhandler实现类,即event的type属性值=handler的name属性值对应的handler中class属性值对应的eventhandler    EventHandler eventHandler = eventFactory.getEventHandler(event.type);    //调用处理句柄的invoke方法        String eventReturn = eventHandler.invoke(event, requestMap, request, response);        if (Debug.verboseOn() || (Debug.infoOn() && "request".equals(trigger))) Debug.logInfo("Ran Event [" + event.type + ":" + event.path + "#" + event.invoke + "] from [" + trigger + "], result is [" + eventReturn + "]", module);        return eventReturn;    }

      使用举例:

 <request-map uri="updateBlog">        <security https="true" auth="true"/>        <event type="service" invoke="updateContent"/>        <response name="success" type="view" value="blogMain"/>        <response name="error" type="view" value="EditBlog"/> </request-map>

    调用解析:

public class ServiceEventHandler implements EventHandler {    public static final String module = ServiceEventHandler.class.getName();    public static final String SYNC = "sync";    public static final String ASYNC = "async";    /**     * @see com.hanlin.fadp.webapp.event.EventHandler#init(javax.servlet.ServletContext)     */    public void init(ServletContext context) throws EventHandlerException {    }    /**     * @author 郑小康     *      * 1.获取本地任务调度器     *     * 2.获取默认mode     *     * 3.获取event节点元素的path 如果为空的话mode赋值为SYNC,不为空就是其path     *     * 4.获取调用服务名,服务名不存在就扔出异常     *     * 5.获取本地化及session     *     * 6.通过调度器根据serviceName获取当前服务ModelService实例     *     * 7.判断是否是二进制请求,是的话获取属性值,注入到multiPartMap     *     * 8.获取request中的参数存入rawParametersMap这个Map     *     * 9.根据参数构造对应的这个map 获取值得优先级multiPartMap Attribute parameter session     *     * 10.调用夫妇服务,若有返回结果将该Map给遍历返回存入到request的Attribute中去     *     * */    public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {        // make sure we have a valid reference to the Service Engine        LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher");        if (dispatcher == null) {            throw new EventHandlerException("The local service dispatcher is null");        }        DispatchContext dctx = dispatcher.getDispatchContext();        if (dctx == null) {            throw new EventHandlerException("Dispatch context cannot be found");        }        // get the details for the service(s) to call        String mode = SYNC;        String serviceName = null;        if (UtilValidate.isEmpty(event.path)) {            mode = SYNC;        } else {            mode = event.path;        }        // make sure we have a defined service to call        serviceName = event.invoke;        if (serviceName == null) {            throw new EventHandlerException("Service name (eventMethod) cannot be null");        }        if (Debug.verboseOn()) Debug.logVerbose("[Set mode/service]: " + mode + "/" + serviceName, module);        // some needed info for when running the service        Locale locale = UtilHttp.getLocale(request);        TimeZone timeZone = UtilHttp.getTimeZone(request);        HttpSession session = request.getSession();        GenericValue userLogin = (GenericValue) session.getAttribute("userLogin");        // get the service model to generate context        ModelService model = null;        try {            model = dctx.getModelService(serviceName);        } catch (GenericServiceException e) {            throw new EventHandlerException("Problems getting the service model", e);        }        if (model == null) {            throw new EventHandlerException("Problems getting the service model");        }        if (Debug.verboseOn()) {            Debug.logVerbose("[Processing]: SERVICE Event", module);            Debug.logVerbose("[Using delegator]: " + dispatcher.getDelegator().getDelegatorName(), module);        }        boolean isMultiPart = ServletFileUpload.isMultipartContent(request);        Map<String, Object> multiPartMap = new HashMap<String, Object>();        if (isMultiPart) {            // get the http upload configuration            String maxSizeStr = EntityUtilProperties.getPropertyValue("general.properties", "http.upload.max.size", "-1", dctx.getDelegator());            long maxUploadSize = -1;            try {                maxUploadSize = Long.parseLong(maxSizeStr);            } catch (NumberFormatException e) {                Debug.logError(e, "Unable to obtain the max upload size from general.properties; using default -1", module);                maxUploadSize = -1;            }            // get the http size threshold configuration - files bigger than this will be            // temporarly stored on disk during upload            String sizeThresholdStr = EntityUtilProperties.getPropertyValue("general.properties", "http.upload.max.sizethreshold", "10240", dctx.getDelegator());            int sizeThreshold = 10240; // 10K            try {                sizeThreshold = Integer.parseInt(sizeThresholdStr);            } catch (NumberFormatException e) {                Debug.logError(e, "Unable to obtain the threshold size from general.properties; using default 10K", module);                sizeThreshold = -1;            }            // directory used to temporarily store files that are larger than the configured size threshold            String tmpUploadRepository = EntityUtilProperties.getPropertyValue("general.properties", "http.upload.tmprepository", "runtime/tmp", dctx.getDelegator());            String encoding = request.getCharacterEncoding();            // check for multipart content types which may have uploaded items            ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory(sizeThreshold, new File(tmpUploadRepository)));            // create the progress listener and add it to the session            FileUploadProgressListener listener = new FileUploadProgressListener();            upload.setProgressListener(listener);            session.setAttribute("uploadProgressListener", listener);            if (encoding != null) {                upload.setHeaderEncoding(encoding);            }            upload.setSizeMax(maxUploadSize);            List<FileItem> uploadedItems = null;            try {                uploadedItems = UtilGenerics.<FileItem>checkList(upload.parseRequest(request));            } catch (FileUploadException e) {                throw new EventHandlerException("Problems reading uploaded data", e);            }            if (uploadedItems != null) {                for (FileItem item: uploadedItems) {                    String fieldName = item.getFieldName();                    if (item.isFormField() || item.getName() == null) {                        if (multiPartMap.containsKey(fieldName)) {                            Object mapValue = multiPartMap.get(fieldName);                            if (mapValue instanceof List<?>) {                                checkList(mapValue, Object.class).add(item.getString());                            } else if (mapValue instanceof String) {                                List<String> newList = new LinkedList<String>();                                newList.add((String) mapValue);                                newList.add(item.getString());                                multiPartMap.put(fieldName, newList);                            } else {                                Debug.logWarning("Form field found [" + fieldName + "] which was not handled!", module);                            }                        } else {                            if (encoding != null) {                                try {                                    multiPartMap.put(fieldName, item.getString(encoding));                                } catch (java.io.UnsupportedEncodingException uee) {                                    Debug.logError(uee, "Unsupported Encoding, using deafault", module);                                    multiPartMap.put(fieldName, item.getString());                                }                            } else {                                multiPartMap.put(fieldName, item.getString());                            }                        }                    } else {                        String fileName = item.getName();                        if (fileName.indexOf('\\') > -1 || fileName.indexOf('/') > -1) {                            // get just the file name IE and other browsers also pass in the local path                            int lastIndex = fileName.lastIndexOf('\\');                            if (lastIndex == -1) {                                lastIndex = fileName.lastIndexOf('/');                            }                            if (lastIndex > -1) {                                fileName = fileName.substring(lastIndex + 1);                            }                        }                        multiPartMap.put(fieldName, ByteBuffer.wrap(item.get()));                        multiPartMap.put("_" + fieldName + "_size", Long.valueOf(item.getSize()));                        multiPartMap.put("_" + fieldName + "_fileName", fileName);                        multiPartMap.put("_" + fieldName + "_contentType", item.getContentType());                    }                }            }        }        // store the multi-part map as an attribute so we can access the parameters        request.setAttribute("multiPartMap", multiPartMap);        Map<String, Object> rawParametersMap = UtilHttp.getCombinedMap(request);        Set<String> urlOnlyParameterNames = UtilHttp.getUrlOnlyParameterMap(request).keySet();        //获取服务中所有参数,并将其注入到serviceContext中去        Map<String, Object> serviceContext = new HashMap<String, Object>();        for (ModelParam modelParam: model.getInModelParamList()) {            String name = modelParam.name;            // don't include userLogin, that's taken care of below            if ("userLogin".equals(name)) continue;            // don't include locale, that is also taken care of below            if ("locale".equals(name)) continue;            // don't include timeZone, that is also taken care of below            if ("timeZone".equals(name)) continue;            Object value = null;            if (UtilValidate.isNotEmpty(modelParam.stringMapPrefix)) {                Map<String, Object> paramMap = UtilHttp.makeParamMapWithPrefix(request, multiPartMap, modelParam.stringMapPrefix, null);                value = paramMap;                if (Debug.verboseOn()) Debug.logVerbose("Set [" + modelParam.name + "]: " + paramMap, module);            } else if (UtilValidate.isNotEmpty(modelParam.stringListSuffix)) {                List<Object> paramList = UtilHttp.makeParamListWithSuffix(request, multiPartMap, modelParam.stringListSuffix, null);                value = paramList;            } else {                //从multiPartMap中获取值                value = multiPartMap.get(name);                //从request.getAttribute中获取值                if (UtilValidate.isEmpty(value)) {                    Object tempVal = request.getAttribute(UtilValidate.isEmpty(modelParam.requestAttributeName) ? name : modelParam.requestAttributeName);                    if (tempVal != null) {                        value = tempVal;                    }                }                //如果依旧为空从参数中获取                if (UtilValidate.isEmpty(value)) {                    ServiceEventHandler.checkSecureParameter(requestMap, urlOnlyParameterNames, name, session, serviceName, dctx.getDelegator());                    // if the service modelParam has allow-html="any" then get this direct from the request instead of in the parameters Map so there will be no canonicalization possibly messing things up                    if ("any".equals(modelParam.allowHtml)) {                        value = request.getParameter(name);                    } else {                        // use the rawParametersMap from UtilHttp in order to also get pathInfo parameters, do canonicalization, etc                        value = rawParametersMap.get(name);                    }                    // make any composite parameter data (e.g., from a set of parameters {name_c_date, name_c_hour, name_c_minutes})                    if (value == null) {                        value = UtilHttp.makeParamValueFromComposite(request, name, locale);                    }                }                //从session获取                if (UtilValidate.isEmpty(value)) {                    Object tempVal = request.getSession().getAttribute(UtilValidate.isEmpty(modelParam.sessionAttributeName) ? name : modelParam.sessionAttributeName);                    if (tempVal != null) {                        value = tempVal;                    }                }                // no field found                if (value == null) {                    //still null, give up for this one                    continue;                }                if (value instanceof String && ((String) value).length() == 0) {                    // interpreting empty fields as null values for each in back end handling...                    value = null;                }            }            //注入serviceContext这个map            serviceContext.put(name, value);        }        // get only the parameters for this service - converted to proper type        // TODO: pass in a list for error messages, like could not convert type or not a proper X, return immediately with messages if there are any        List<Object> errorMessages = new LinkedList<Object>();        serviceContext = model.makeValid(serviceContext, ModelService.IN_PARAM, true, errorMessages, timeZone, locale);        if (errorMessages.size() > 0) {            // uh-oh, had some problems...            request.setAttribute("_ERROR_MESSAGE_LIST_", errorMessages);            return "error";        }        // include the UserLogin value object        if (userLogin != null) {            serviceContext.put("userLogin", userLogin);        }        // include the Locale object        if (locale != null) {            serviceContext.put("locale", locale);        }        // include the TimeZone object        if (timeZone != null) {            serviceContext.put("timeZone", timeZone);        }        //调用服务        Map<String, Object> result = null;        try {            if (ASYNC.equalsIgnoreCase(mode)) {                dispatcher.runAsync(serviceName, serviceContext);            } else {                result = dispatcher.runSync(serviceName, serviceContext);            }        } catch (ServiceAuthException e) {            // not logging since the service engine already did            request.setAttribute("_ERROR_MESSAGE_", e.getNonNestedMessage());            return "error";        } catch (ServiceValidationException e) {            // not logging since the service engine already did            request.setAttribute("serviceValidationException", e);            if (e.getMessageList() != null) {                request.setAttribute("_ERROR_MESSAGE_LIST_", e.getMessageList());            } else {                request.setAttribute("_ERROR_MESSAGE_", e.getNonNestedMessage());            }            return "error";        } catch (GenericServiceException e) {            Debug.logError(e, "Service invocation error", module);            throw new EventHandlerException("Service invocation error", e.getNested());        }        String responseString = null;        if (result == null) {            responseString = ModelService.RESPOND_SUCCESS;        } else {            if (!result.containsKey(ModelService.RESPONSE_MESSAGE)) {                responseString = ModelService.RESPOND_SUCCESS;            } else {                responseString = (String) result.get(ModelService.RESPONSE_MESSAGE);            }            // set the messages in the request; this will be picked up by messages.ftl and displayed            request.setAttribute("_ERROR_MESSAGE_LIST_", result.get(ModelService.ERROR_MESSAGE_LIST));            request.setAttribute("_ERROR_MESSAGE_MAP_", result.get(ModelService.ERROR_MESSAGE_MAP));            request.setAttribute("_ERROR_MESSAGE_", result.get(ModelService.ERROR_MESSAGE));            request.setAttribute("_EVENT_MESSAGE_LIST_", result.get(ModelService.SUCCESS_MESSAGE_LIST));            request.setAttribute("_EVENT_MESSAGE_", result.get(ModelService.SUCCESS_MESSAGE));            // set the results in the request            for (Map.Entry<String, Object> rme: result.entrySet()) {                String resultKey = rme.getKey();                Object resultValue = rme.getValue();                if (resultKey != null && !ModelService.RESPONSE_MESSAGE.equals(resultKey) && !ModelService.ERROR_MESSAGE.equals(resultKey) &&                        !ModelService.ERROR_MESSAGE_LIST.equals(resultKey) && !ModelService.ERROR_MESSAGE_MAP.equals(resultKey) &&                        !ModelService.SUCCESS_MESSAGE.equals(resultKey) && !ModelService.SUCCESS_MESSAGE_LIST.equals(resultKey)) {                    request.setAttribute(resultKey, resultValue);                }            }        }        if (Debug.verboseOn()) Debug.logVerbose("[Event Return]: " + responseString, module);        return responseString;    }    public static void checkSecureParameter(RequestMap requestMap, Set<String> urlOnlyParameterNames, String name, HttpSession session, String serviceName, Delegator delegator) throws EventHandlerException {        // special case for security: if this is a request-map defined as secure in controller.xml then only accept body parameters coming in, ie don't allow the insecure URL parameters        // NOTE: the RequestHandler will check the HttpSerletRequest security to make sure it is secure if the request-map -> security -> https=true, but we can't just look at the request.isSecure() method here because it is allowed to send secure requests for request-map with https=false        if (requestMap != null && requestMap.securityHttps) {            if (urlOnlyParameterNames.contains(name)) {                String errMsg = "Found URL parameter [" + name + "] passed to secure (https) request-map with uri ["                    + requestMap.uri + "] with an event that calls service ["                    + serviceName + "]; this is not allowed for security reasons! The data should be encrypted by making it part of the request body "                    + "(a form field) instead of the request URL."                    + " Moreover it would be kind if you could create a Jira sub-task of https://issues.apache.org/jira/browse/OFBIZ-2330 "                    + "(check before if a sub-task for this error does not exist)."                    + " If you are not sure how to create a Jira issue please have a look before at http://cwiki.apache.org/confluence/x/JIB2"                    + " Thank you in advance for your help.";                Debug.logError("=============== " + errMsg + "; In session [" + session.getId() + "]; Note that this can be changed using the service.http.parameters.require.encrypted property in the url.properties file", module);                // the default here is true, so anything but N/n is true                boolean requireEncryptedServiceWebParameters = !EntityUtilProperties.propertyValueEqualsIgnoreCase("url.properties", "service.http.parameters.require.encrypted", "N", delegator);                // NOTE: this forces service call event parameters to be in the body and not in the URL! can be issues with existing links, like Delete links or whatever, and those need to be changed to forms!                if (requireEncryptedServiceWebParameters) {                    throw new EventHandlerException(errMsg);                }            }            // NOTTODO: may want to allow parameters that map to entity PK fields to be in the URL, but that might be a big security hole since there are certain security sensitive entities that are made of only PK fields, or that only need PK fields to function (like UserLoginSecurityGroup)            // NOTTODO: we could allow URL parameters when it is not a POST (ie when !request.getMethod().equalsIgnoreCase("POST")), but that would open a security hole where sensitive parameters can be passed on the URL in a GET/etc and bypass this security constraint        }    }}


原创粉丝点击