net.ymate.platform.mvc.web.support.RequestMappingParser.java Source code

Java tutorial

Introduction

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

Source

/*
 * Copyright 2007-2107 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.mvc.web.support;

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

import net.ymate.platform.commons.lang.PairObject;
import net.ymate.platform.mvc.context.IRequestContext;
import net.ymate.platform.mvc.web.context.WebContext;

import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * RequestMappingParser
 * </p>
 * <p>
 * WebMVC??
 * </p>
 * 
 * @author (suninformation@163.com)
 * @version 0.0.0
 *          <table style="border:1px solid gray;">
 *          <tr>
 *          <th width="100px">?</th><th width="100px"></th><th
 *          width="100px"></th><th width="100px"></th>
 *          </tr>
 *          <!--  Table ?? -->
 *          <tr>
 *          <td>0.0.0</td>
 *          <td></td>
 *          <td></td>
 *          <td>2011-7-26?11:11:45</td>
 *          </tr>
 *          </table>
 */
public class RequestMappingParser {

    /**
     * @param partStr ?
     * @return '/'
     */
    protected String fixMappingPart(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????
     */
    protected Map<String, String> parserMappingParams(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 
     * @param mappingSet ???'$'?
     * @return ?????????WebContextPathVariable?
     */
    public String doParser(IRequestContext context, Set<String> mappingSet) {
        String _requestMapping = this.fixMappingPart(context.getRequestMapping());
        // ??PairObject<Mapping[??], ??>
        Set<PairObject<String[], Integer>> _filteredMapping = new HashSet<PairObject<String[], Integer>>();
        for (String _key : mappingSet) {
            if (_key.contains("{")) {
                String _mappingKey = StringUtils.substringBefore(_key, "{");
                String _fixedMappingKey = this.fixMappingPart(_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.parserMappingParams(_mappingParamPart, _result.getKey()[3]);
            if (WebContext.getContext() != null) {
                // ???WebContextPathVariable?
                for (String _key : _params.keySet()) {
                    WebContext.getContext().put(_key, _params.get(_key));
                }
            }
            return _result.getKey()[0];
        }
        return null;
    }

}