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: */
019:
020: package org.apache.axis2.dataretrieval;
021:
022: import java.io.StringReader;
023: import java.io.StringWriter;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026:
027: import javax.xml.stream.XMLStreamException;
028:
029: import org.apache.axiom.om.OMElement;
030: import org.apache.axiom.om.OMNode;
031: import org.apache.axis2.context.MessageContext;
032: import org.apache.axis2.description.AxisService;
033: import org.apache.axis2.util.XMLUtils;
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036: import org.apache.ws.commons.schema.XmlSchema;
037:
038: /**
039: * Axis 2 Data Locator responsibles for retrieving Schema metadata. The class is
040: * created as model for schema specific data locator; and also easier for any
041: * future implementation schema specific data retrieval logic.
042: */
043:
044: public class SchemaDataLocator extends BaseAxisDataLocator implements
045: AxisDataLocator {
046:
047: private String requestIdentifier = null;
048:
049: private String serviceEPR = null;
050:
051: /**
052: *
053: */
054: private static final Log LOG = LogFactory
055: .getLog(SchemaDataLocator.class.getClass().getName());
056:
057: protected SchemaDataLocator() {
058:
059: }
060:
061: /**
062: * Constructor
063: */
064: protected SchemaDataLocator(ServiceData[] data) {
065: dataList = data;
066: }
067:
068: public Data[] getData(DataRetrievalRequest request,
069: MessageContext msgContext) throws DataRetrievalException {
070:
071: requestIdentifier = request.getIdentifier();
072: serviceEPR = msgContext.getTo().getAddress();
073:
074: OutputForm outputForm = request.getOutputForm();
075: if (outputForm == null) {
076: outputForm = OutputForm.INLINE_FORM;
077: }
078:
079: Data[] data;
080:
081: if (outputForm == OutputForm.INLINE_FORM) {
082: data = outputInlineForm(msgContext, dataList);
083:
084: } else if (outputForm == OutputForm.LOCATION_FORM) {
085: data = outputLocationForm(dataList);
086:
087: } else {
088: data = outputReferenceForm(msgContext, dataList);
089: }
090:
091: return data;
092: }
093:
094: protected Data[] outputInlineForm(MessageContext msgContext,
095: ServiceData[] serviceData) throws DataRetrievalException {
096:
097: Data[] data = super .outputInlineForm(msgContext, serviceData);
098:
099: if (data.length != 0) {
100: return data;
101: }
102:
103: AxisService axisService = msgContext.getAxisService();
104: ArrayList schemaList = axisService.getSchema();
105:
106: ArrayList results = new ArrayList();
107: XmlSchema schema;
108:
109: for (Iterator iterator = schemaList.iterator(); iterator
110: .hasNext();) {
111: schema = (XmlSchema) iterator.next();
112:
113: if (requestIdentifier != null) {
114: if (requestIdentifier.equals(schema
115: .getTargetNamespace())) {
116: results.add(new Data(convertToOM(schema),
117: requestIdentifier));
118: }
119: } else {
120: results.add(new Data(convertToOM(schema), null));
121: }
122: }
123:
124: return (Data[]) results.toArray(new Data[results.size()]);
125: }
126:
127: protected Data[] outputLocationForm(ServiceData[] serviceData)
128: throws DataRetrievalException {
129:
130: Data[] data = super .outputLocationForm(serviceData);
131:
132: if (data != null && data.length != 0) {
133: return data;
134: }
135: return new Data[] { new Data(serviceEPR + "?xsd",
136: requestIdentifier) };
137: }
138:
139: private OMNode convertToOM(XmlSchema schema)
140: throws DataRetrievalException {
141: StringWriter writer = new StringWriter();
142: schema.write(writer);
143:
144: StringReader reader = new StringReader(writer.toString());
145: try {
146: return XMLUtils.toOM(reader);
147: } catch (XMLStreamException e) {
148: throw new DataRetrievalException(
149: "Can't convert XmlSchema object to an OMElement", e);
150: }
151: }
152: }
|