001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */package org.apache.cxf.binding.http;
019:
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.apache.cxf.message.Message;
026: import org.apache.cxf.service.model.BindingOperationInfo;
027: import org.apache.cxf.service.model.MessageInfo;
028: import org.apache.cxf.service.model.MessagePartInfo;
029: import org.apache.cxf.service.model.OperationInfo;
030: import org.codehaus.jra.ResourceUtil;
031:
032: public class URIMapper {
033: private List<ResourceInfo> resources = new ArrayList<ResourceInfo>();
034: private Map<OperationInfo, String> locations = new HashMap<OperationInfo, String>();
035: private Map<OperationInfo, String> verbs = new HashMap<OperationInfo, String>();
036:
037: public BindingOperationInfo getOperation(String uri, String verb,
038: Message m) {
039: ResourceInfo bestMatch = null;
040: int bestScore = 0;
041: for (ResourceInfo r : resources) {
042: if (r.getVerb().equals(verb)) {
043: int newScore = ResourceUtil.getMatchScore(uri, r
044: .getUri());
045: if (newScore > bestScore) {
046: bestMatch = r;
047: bestScore = newScore;
048: }
049: }
050: }
051:
052: if (bestScore > -1 && bestMatch != null) {
053: return bestMatch.getOperation();
054: }
055:
056: return null;
057: }
058:
059: public void bind(BindingOperationInfo bop, String uri, String verb) {
060: ResourceInfo info = new ResourceInfo();
061: info.setUri(uri);
062: info.setVerb(verb);
063: info.setOperation(bop);
064: locations.put(bop.getOperationInfo(), uri);
065: verbs.put(bop.getOperationInfo(), verb);
066: resources.add(info);
067: }
068:
069: public String getLocation(BindingOperationInfo bop) {
070: return locations.get(bop.getOperationInfo());
071: }
072:
073: public String getVerb(BindingOperationInfo bop) {
074: return verbs.get(bop.getOperationInfo());
075: }
076:
077: public static class ResourceInfo {
078: private String uri;
079: private String verb;
080: private BindingOperationInfo operation;
081:
082: public BindingOperationInfo getOperation() {
083: return operation;
084: }
085:
086: public void setOperation(BindingOperationInfo operation) {
087: this .operation = operation;
088: }
089:
090: public String getUri() {
091: return uri;
092: }
093:
094: public void setUri(String uri) {
095: this .uri = uri;
096: }
097:
098: public String getVerb() {
099: return verb;
100: }
101:
102: public void setVerb(String verb) {
103: this .verb = verb;
104: }
105: }
106:
107: public List getParameters(MessageInfo msgInfo, String path) {
108: String resource = locations.get(msgInfo.getOperation());
109: Map<String, String> paramMap = ResourceUtil.getURIParameters(
110: path, resource);
111:
112: List<Object> params = new ArrayList<Object>(msgInfo
113: .getMessageParts().size());
114:
115: int i = 0;
116: for (MessagePartInfo p : msgInfo.getMessageParts()) {
117: params.add(i, paramMap.get(p.getName().getLocalPart()));
118: i++;
119: }
120:
121: return params;
122: }
123: }
|