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.interceptor;
019:
020: import org.w3c.dom.Document;
021: import org.w3c.dom.Element;
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024:
025: import org.apache.cxf.binding.http.URIMapper;
026: import org.apache.cxf.endpoint.Endpoint;
027: import org.apache.cxf.helpers.DOMUtils;
028: import org.apache.cxf.interceptor.Fault;
029: import org.apache.cxf.interceptor.MessageSenderInterceptor;
030: import org.apache.cxf.message.Message;
031: import org.apache.cxf.phase.AbstractPhaseInterceptor;
032: import org.apache.cxf.phase.Phase;
033: import org.apache.cxf.service.model.BindingOperationInfo;
034:
035: /**
036: * Sets up the destination URI for a client invocation.
037: */
038: public class URIParameterOutInterceptor extends
039: AbstractPhaseInterceptor<Message> {
040:
041: public URIParameterOutInterceptor() {
042: super (Phase.PREPARE_SEND);
043: addBefore(MessageSenderInterceptor.class.getName());
044: }
045:
046: public void handleMessage(Message message) throws Fault {
047: Endpoint ep = message.getExchange().get(Endpoint.class);
048: URIMapper mapper = (URIMapper) ep.getService().get(
049: URIMapper.class.getName());
050: BindingOperationInfo bop = message.getExchange().get(
051: BindingOperationInfo.class);
052:
053: String address = ep.getEndpointInfo().getAddress();
054: String location = mapper.getLocation(bop);
055:
056: StringBuilder uri = new StringBuilder();
057: uri.append(address);
058:
059: boolean addressSlash = address.charAt(address.length() - 1) == '/';
060: boolean locationSlash = location.charAt(0) == '/';
061: if (!addressSlash && !locationSlash) {
062: uri.append('/');
063: uri.append(location);
064: } else if (addressSlash && locationSlash) {
065: uri.append(location.substring(1));
066: } else {
067: uri.append(location);
068: }
069:
070: Document d = (Document) message.getContent(Node.class);
071: String encodedUri = encodeIri(uri.toString(), d);
072:
073: message.put(Message.ENDPOINT_ADDRESS, encodedUri);
074: }
075:
076: public static String encodeIri(String uri, Document doc) {
077: StringBuilder builder = new StringBuilder();
078: String locPath = uri;
079: Element root = doc.getDocumentElement();
080:
081: int start = 0;
082: char c;
083: for (int idx1 = 0; idx1 < locPath.length(); idx1++) {
084: c = locPath.charAt(idx1);
085: if (c == '{') {
086: if (locPath.charAt(idx1 + 1) == '{') {
087: idx1++;
088: } else {
089: builder.append(locPath.substring(start, idx1));
090:
091: int locEnd = locPath.indexOf('}', idx1);
092: String name = locPath.substring(idx1 + 1, locEnd);
093: idx1 = locEnd;
094:
095: NodeList childNodes = root.getChildNodes();
096: for (int i = 0; i < childNodes.getLength(); i++) {
097: Node n = childNodes.item(i);
098:
099: if (n.getNodeType() == Node.ELEMENT_NODE
100: && name.equals(n.getLocalName())) {
101: builder.append(DOMUtils.getContent(n));
102: break;
103: }
104: }
105:
106: start = locEnd + 1;
107: }
108: }
109: }
110:
111: if (start == 0) {
112: return uri;
113: }
114:
115: return builder.toString();
116: }
117:
118: }
|