net.ymate.platform.webmvc.support.RequestMappingParser.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.platform.webmvc.support.RequestMappingParser.java

Source

/*
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.platform.webmvc.support;

import net.ymate.platform.core.lang.PairObject;
import net.ymate.platform.webmvc.IRequestContext;
import net.ymate.platform.webmvc.RequestMeta;
import net.ymate.platform.webmvc.base.Type;
import org.apache.commons.lang.StringUtils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * WebMVC?
 *
 * @author  (suninformation@163.com) on 2011-7-26 ?11:11:45
 * @version 1.0
 */
public class RequestMappingParser {

    private Map<String, RequestMeta> __MAPPING_META_FOR_GET;

    private Map<String, RequestMeta> __MAPPING_META_FOR_POST;

    private Map<String, RequestMeta> __MAPPING_META_FOR_DELETE;

    private Map<String, RequestMeta> __MAPPING_META_FOR_PUT;

    private Map<String, RequestMeta> __MAPPING_META_FOR_OPTIONS;

    private Map<String, RequestMeta> __MAPPING_META_FOR_HEAD;

    private Map<String, RequestMeta> __MAPPING_META_FOR_TRACE;

    public RequestMappingParser() {
        __MAPPING_META_FOR_GET = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_POST = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_DELETE = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_PUT = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_OPTIONS = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_HEAD = new HashMap<String, RequestMeta>();
        __MAPPING_META_FOR_TRACE = new HashMap<String, RequestMeta>();
    }

    public void registerRequestMeta(RequestMeta requestMeta) {
        for (Type.HttpMethod _method : requestMeta.getAllowMethods()) {
            switch (_method) {
            case POST:
                __MAPPING_META_FOR_POST.put(requestMeta.getMapping(), requestMeta);
                break;
            case DELETE:
                __MAPPING_META_FOR_DELETE.put(requestMeta.getMapping(), requestMeta);
                break;
            case PUT:
                __MAPPING_META_FOR_PUT.put(requestMeta.getMapping(), requestMeta);
                break;
            case OPTIONS:
                __MAPPING_META_FOR_OPTIONS.put(requestMeta.getMapping(), requestMeta);
                break;
            case HEAD:
                __MAPPING_META_FOR_HEAD.put(requestMeta.getMapping(), requestMeta);
                break;
            case TRACE:
                __MAPPING_META_FOR_TRACE.put(requestMeta.getMapping(), requestMeta);
                break;
            default:
                __MAPPING_META_FOR_GET.put(requestMeta.getMapping(), requestMeta);
            }
        }
    }

    /**
     * @param partStr ?
     * @return '/'
     */
    private String __doFixMappingPart(String partStr) {
        partStr = StringUtils.trimToEmpty(partStr);
        if (StringUtils.startsWith(partStr, "/")) {
            partStr = StringUtils.substringAfter(partStr, "/");
        }
        if (StringUtils.endsWith(partStr, "/")) {
            partStr = StringUtils.substringBeforeLast(partStr, "/");
        }
        return partStr;
    }

    /**
     * @param mappingParamPart ?
     * @param mappingMatcher   ??
     * @return ?mappingMatcher????
     */
    private Map<String, String> __doParserMappingParams(String mappingParamPart, String mappingMatcher) {
        Map<String, String> _params = new HashMap<String, String>();
        String[] _paramNames = StringUtils.split(mappingMatcher, "/");
        String[] _parts = StringUtils.split(mappingParamPart, "/");
        if (_parts.length >= _paramNames.length) {
            for (int _index = 0; _index < _paramNames.length; _index++) {
                String _pName = StringUtils.substringBetween(_paramNames[_index], "{", "}");
                if (_pName != null) {
                    _params.put(_pName, _parts[_index]);
                }
            }
        }
        return _params;
    }

    /**
     * @param context 
     * @return ?????????WebContextPathVariable?
     */
    public RequestMeta doParse(IRequestContext context) {
        RequestMeta _meta = null;
        Map<String, RequestMeta> _mappingMap = null;
        switch (context.getHttpMethod()) {
        case POST:
            _mappingMap = __MAPPING_META_FOR_POST;
            break;
        case DELETE:
            _mappingMap = __MAPPING_META_FOR_DELETE;
            break;
        case PUT:
            _mappingMap = __MAPPING_META_FOR_PUT;
            break;
        case OPTIONS:
            _mappingMap = __MAPPING_META_FOR_OPTIONS;
            break;
        case HEAD:
            _mappingMap = __MAPPING_META_FOR_HEAD;
            break;
        case TRACE:
            _mappingMap = __MAPPING_META_FOR_TRACE;
            break;
        default:
            _mappingMap = __MAPPING_META_FOR_GET;
        }
        _meta = _mappingMap.get(context.getRequestMapping());
        if (_meta == null) {
            String _requestMapping = this.__doFixMappingPart(context.getRequestMapping());
            // ??PairObject<Mapping[??], ??>
            Set<PairObject<String[], Integer>> _filteredMapping = new HashSet<PairObject<String[], Integer>>();
            for (String _key : _mappingMap.keySet()) {
                if (_key.contains("{")) {
                    String _mappingKey = StringUtils.substringBefore(_key, "{");
                    String _fixedMappingKey = this.__doFixMappingPart(_mappingKey);
                    if (StringUtils.startsWithIgnoreCase(_requestMapping, _fixedMappingKey)) {
                        // ?????
                        String _paramKey = _key.substring(_key.indexOf('{'));
                        _filteredMapping.add(new PairObject<String[], Integer>(
                                new String[] { _key, _mappingKey, _fixedMappingKey, _paramKey },
                                StringUtils.split(_paramKey, "/").length));
                    }
                }
            }
            // ?????
            PairObject<String[], Integer> _result = null;
            String _mappingParamPart = null;
            for (PairObject<String[], Integer> _item : _filteredMapping) {
                // ?????
                _mappingParamPart = StringUtils.substringAfter(_requestMapping, _item.getKey()[2]);
                // ???
                int _paramPartCount = StringUtils.split(_mappingParamPart, "/").length;
                // ?????
                if (_paramPartCount == _item.getValue()) {
                    _result = _item;
                    break;
                }
            }
            if (_result != null) {
                Map<String, String> _params = this.__doParserMappingParams(_mappingParamPart, _result.getKey()[3]);
                // ???WebContextPathVariable?
                for (Map.Entry<String, String> _entry : _params.entrySet()) {
                    context.addAttribute(_entry.getKey(), _entry.getValue());
                }
                _meta = _mappingMap.get(_result.getKey()[0]);
            }
        }
        return _meta;
    }
}