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.synapse.core.axis2;
021:
022: import org.apache.axiom.om.OMElement;
023: import org.apache.axiom.soap.SOAPEnvelope;
024: import org.apache.axiom.soap.SOAPHeader;
025: import org.apache.axiom.soap.SOAPHeaderBlock;
026: import org.apache.axis2.AxisFault;
027: import org.apache.axis2.Constants;
028: import org.apache.axis2.addressing.EndpointReference;
029: import org.apache.axis2.addressing.RelatesTo;
030: import org.apache.commons.logging.Log;
031: import org.apache.commons.logging.LogFactory;
032: import org.apache.synapse.FaultHandler;
033: import org.apache.synapse.Mediator;
034: import org.apache.synapse.MessageContext;
035: import org.apache.synapse.SynapseConstants;
036: import org.apache.synapse.config.Entry;
037: import org.apache.synapse.config.SynapseConfiguration;
038: import org.apache.synapse.core.SynapseEnvironment;
039: import org.apache.synapse.endpoints.Endpoint;
040: import org.apache.synapse.mediators.MediatorFaultHandler;
041:
042: import java.util.*;
043:
044: /**
045: * This is the MessageContext implementation that synapse uses almost all the time because Synapse
046: * is implemented on top of the Axis2
047: */
048: public class Axis2MessageContext implements MessageContext {
049:
050: private static final Log log = LogFactory
051: .getLog(Axis2MessageContext.class);
052:
053: /** Holds the reference to the Synapse Message Context */
054: private SynapseConfiguration synCfg = null;
055:
056: /** Holds the environment on which synapse operates */
057: private SynapseEnvironment synEnv = null;
058:
059: /** Synapse Message Context properties */
060: private Map properties = new HashMap();
061:
062: /**
063: * Local entries fetched from the configuration or from the registry for the transactional
064: * resource access
065: */
066: private Map localEntries = new HashMap();
067:
068: /** Fault Handler stack which will be popped and called the handleFault in error states */
069: private Stack faultStack = new Stack();
070:
071: /** The Axis2 MessageContext reference */
072: private org.apache.axis2.context.MessageContext axis2MessageContext = null;
073:
074: /** Attribute of the MC specifying whether this is a response or not */
075: private boolean response = false;
076:
077: /** Attribute specifying whether this MC corresponds to fault response or not */
078: private boolean faultResponse = false;
079:
080: /** Attribute of MC stating the tracing state of the message */
081: private int tracingState = SynapseConstants.TRACING_UNSET;
082:
083: /** The service log for this message */
084: private Log serviceLog = null;
085:
086: public SynapseConfiguration getConfiguration() {
087: return synCfg;
088: }
089:
090: public void setConfiguration(SynapseConfiguration synCfg) {
091: this .synCfg = synCfg;
092: }
093:
094: public SynapseEnvironment getEnvironment() {
095: return synEnv;
096: }
097:
098: public void setEnvironment(SynapseEnvironment synEnv) {
099: this .synEnv = synEnv;
100: }
101:
102: public Map getContextEntries() {
103: return localEntries;
104: }
105:
106: public void setContextEntries(Map entries) {
107: this .localEntries = entries;
108: }
109:
110: public Mediator getMainSequence() {
111: Object o = localEntries.get(SynapseConstants.MAIN_SEQUENCE_KEY);
112: if (o != null && o instanceof Mediator) {
113: return (Mediator) o;
114: } else {
115: Mediator main = getConfiguration().getMainSequence();
116: localEntries.put(SynapseConstants.MAIN_SEQUENCE_KEY, main);
117: return main;
118: }
119: }
120:
121: public Mediator getFaultSequence() {
122: Object o = localEntries
123: .get(SynapseConstants.FAULT_SEQUENCE_KEY);
124: if (o != null && o instanceof Mediator) {
125: return (Mediator) o;
126: } else {
127: Mediator fault = getConfiguration().getFaultSequence();
128: localEntries
129: .put(SynapseConstants.FAULT_SEQUENCE_KEY, fault);
130: return fault;
131: }
132: }
133:
134: public Mediator getSequence(String key) {
135: Object o = localEntries.get(key);
136: if (o != null && o instanceof Mediator) {
137: return (Mediator) o;
138: } else {
139: Mediator m = getConfiguration().getSequence(key);
140: localEntries.put(key, m);
141: return m;
142: }
143: }
144:
145: public Endpoint getEndpoint(String key) {
146: Object o = localEntries.get(key);
147: if (o != null && o instanceof Endpoint) {
148: return (Endpoint) o;
149: } else {
150: Endpoint e = getConfiguration().getEndpoint(key);
151: localEntries.put(key, e);
152: return e;
153: }
154: }
155:
156: public Object getEntry(String key) {
157: Object o = localEntries.get(key);
158: if (o != null && o instanceof Entry) {
159: return ((Entry) o).getValue();
160: } else {
161: Object e = getConfiguration().getEntry(key);
162: if (e != null) {
163: localEntries.put(key, e);
164: return e;
165: } else {
166: getConfiguration().getEntryDefinition(key);
167: return getConfiguration().getEntry(key);
168: }
169: }
170: }
171:
172: public Object getProperty(String key) {
173: return properties.get(key);
174: }
175:
176: public void setProperty(String key, Object value) {
177: properties.put(key, value);
178:
179: // do not commit response by default in the server process
180: if (SynapseConstants.RESPONSE.equals(key)
181: && getAxis2MessageContext().getOperationContext() != null) {
182: getAxis2MessageContext()
183: .getOperationContext()
184: .setProperty(
185: org.apache.axis2.Constants.RESPONSE_WRITTEN,
186: "SKIP");
187: }
188: }
189:
190: public Set getPropertyKeySet() {
191: return properties.keySet();
192: }
193:
194: /**
195: * Constructor for the Axis2MessageContext inside Synapse
196: *
197: * @param axisMsgCtx MessageContext representing the relevant Axis MC
198: * @param synCfg SynapseConfiguraion describing Synapse
199: * @param synEnv SynapseEnvironment describing the environment of Synapse
200: */
201: public Axis2MessageContext(
202: org.apache.axis2.context.MessageContext axisMsgCtx,
203: SynapseConfiguration synCfg, SynapseEnvironment synEnv) {
204: setAxis2MessageContext(axisMsgCtx);
205: this .synCfg = synCfg;
206: this .synEnv = synEnv;
207: this .pushFaultHandler(new MediatorFaultHandler(synCfg
208: .getFaultSequence()));
209: }
210:
211: public EndpointReference getFaultTo() {
212: return axis2MessageContext.getFaultTo();
213: }
214:
215: public void setFaultTo(EndpointReference reference) {
216: axis2MessageContext.setFaultTo(reference);
217: }
218:
219: public EndpointReference getFrom() {
220: return axis2MessageContext.getFrom();
221: }
222:
223: public void setFrom(EndpointReference reference) {
224: axis2MessageContext.setFrom(reference);
225: }
226:
227: public SOAPEnvelope getEnvelope() {
228: return axis2MessageContext.getEnvelope();
229: }
230:
231: public void setEnvelope(SOAPEnvelope envelope) throws AxisFault {
232: axis2MessageContext.setEnvelope(envelope);
233: }
234:
235: public String getMessageID() {
236: return axis2MessageContext.getMessageID();
237: }
238:
239: public void setMessageID(String string) {
240: axis2MessageContext.setMessageID(string);
241: }
242:
243: public RelatesTo getRelatesTo() {
244: return axis2MessageContext.getRelatesTo();
245: }
246:
247: public void setRelatesTo(RelatesTo[] reference) {
248: axis2MessageContext.setRelationships(reference);
249: }
250:
251: public EndpointReference getReplyTo() {
252: return axis2MessageContext.getReplyTo();
253: }
254:
255: public void setReplyTo(EndpointReference reference) {
256: axis2MessageContext.setReplyTo(reference);
257: }
258:
259: public EndpointReference getTo() {
260: return axis2MessageContext.getTo();
261: }
262:
263: public void setTo(EndpointReference reference) {
264: axis2MessageContext.setTo(reference);
265: }
266:
267: public void setWSAAction(String actionURI) {
268: axis2MessageContext.setWSAAction(actionURI);
269: }
270:
271: public String getWSAAction() {
272: return axis2MessageContext.getWSAAction();
273: }
274:
275: public void setWSAMessageID(String messageID) {
276: axis2MessageContext.setWSAMessageId(messageID);
277: }
278:
279: public String getWSAMessageID() {
280: return axis2MessageContext.getMessageID();
281: }
282:
283: public String getSoapAction() {
284: return axis2MessageContext.getSoapAction();
285: }
286:
287: public void setSoapAction(String string) {
288: axis2MessageContext.setSoapAction(string);
289: }
290:
291: public boolean isDoingMTOM() {
292: return axis2MessageContext.isDoingMTOM();
293: }
294:
295: public boolean isDoingSWA() {
296: return axis2MessageContext.isDoingSwA();
297: }
298:
299: public void setDoingMTOM(boolean b) {
300: axis2MessageContext.setDoingMTOM(b);
301: }
302:
303: public void setDoingSWA(boolean b) {
304: axis2MessageContext.setDoingSwA(b);
305: }
306:
307: public boolean isDoingPOX() {
308: return axis2MessageContext.isDoingREST();
309: }
310:
311: public void setDoingPOX(boolean b) {
312: axis2MessageContext.setDoingREST(b);
313: }
314:
315: public boolean isDoingGET() {
316: return Constants.Configuration.HTTP_METHOD_GET
317: .equals(axis2MessageContext
318: .getProperty(Constants.Configuration.HTTP_METHOD))
319: && axis2MessageContext.isDoingREST();
320: }
321:
322: public void setDoingGET(boolean b) {
323: if (b) {
324: axis2MessageContext.setDoingREST(b);
325: axis2MessageContext.setProperty(
326: Constants.Configuration.HTTP_METHOD,
327: Constants.Configuration.HTTP_METHOD_GET);
328: } else {
329: axis2MessageContext
330: .removeProperty(Constants.Configuration.HTTP_METHOD);
331: }
332: }
333:
334: public boolean isSOAP11() {
335: return axis2MessageContext.isSOAP11();
336: }
337:
338: public void setResponse(boolean b) {
339: response = b;
340: axis2MessageContext.setProperty(
341: SynapseConstants.ISRESPONSE_PROPERTY, b);
342: }
343:
344: public boolean isResponse() {
345: Object o = properties.get(SynapseConstants.RESPONSE);
346: if (o != null && o instanceof String
347: && ((String) o).equalsIgnoreCase("true")) {
348: return true;
349: }
350: return response;
351: }
352:
353: public void setFaultResponse(boolean b) {
354: this .faultResponse = b;
355: }
356:
357: public boolean isFaultResponse() {
358: return this .faultResponse;
359: }
360:
361: public int getTracingState() {
362: return tracingState;
363: }
364:
365: public void setTracingState(int tracingState) {
366: this .tracingState = tracingState;
367: }
368:
369: public Stack getFaultStack() {
370: return this .faultStack;
371: }
372:
373: public void pushFaultHandler(FaultHandler fault) {
374: this .faultStack.push(fault);
375: }
376:
377: /**
378: * Return the service level Log for this message context or null
379: * @return the service level Log for the message
380: */
381: public Log getServiceLog() {
382:
383: if (serviceLog != null) {
384: return serviceLog;
385: } else {
386: String serviceName = (String) getProperty(SynapseConstants.PROXY_SERVICE);
387: if (serviceName != null
388: && synCfg.getProxyService(serviceName) != null) {
389: serviceLog = LogFactory
390: .getLog(SynapseConstants.SERVICE_LOGGER_PREFIX
391: + serviceName);
392: return serviceLog;
393: } else {
394: serviceLog = LogFactory
395: .getLog(SynapseConstants.SERVICE_LOGGER_PREFIX
396: .substring(
397: 0,
398: SynapseConstants.SERVICE_LOGGER_PREFIX
399: .length() - 1));
400: return serviceLog;
401: }
402: }
403: }
404:
405: /**
406: * Set the service log
407: * @param serviceLog
408: */
409: public void setServiceLog(Log serviceLog) {
410: this .serviceLog = serviceLog;
411: }
412:
413: public org.apache.axis2.context.MessageContext getAxis2MessageContext() {
414: return axis2MessageContext;
415: }
416:
417: public void setAxis2MessageContext(
418: org.apache.axis2.context.MessageContext axisMsgCtx) {
419: this .axis2MessageContext = axisMsgCtx;
420: Boolean resp = (Boolean) axisMsgCtx
421: .getProperty(SynapseConstants.ISRESPONSE_PROPERTY);
422: if (resp != null)
423: response = resp.booleanValue();
424: }
425:
426: public void setPaused(boolean value) {
427: axis2MessageContext.setPaused(value);
428: }
429:
430: public boolean isPaused() {
431: return axis2MessageContext.isPaused();
432: }
433:
434: public boolean isServerSide() {
435: return axis2MessageContext.isServerSide();
436: }
437:
438: public void setServerSide(boolean value) {
439: axis2MessageContext.setServerSide(value);
440: }
441:
442: public String toString() {
443: StringBuffer sb = new StringBuffer();
444: String separator = "\n";
445:
446: if (getTo() != null)
447: sb.append("To: ").append(getTo().getAddress());
448: else
449: sb.append("To: ");
450: if (getFrom() != null)
451: sb.append(separator).append("From: ").append(
452: getFrom().getAddress());
453: if (getWSAAction() != null)
454: sb.append(separator).append("WSAction: ").append(
455: getWSAAction());
456: if (getSoapAction() != null)
457: sb.append(separator).append("SOAPAction: ").append(
458: getSoapAction());
459: if (getReplyTo() != null)
460: sb.append(separator).append("ReplyTo: ").append(
461: getReplyTo().getAddress());
462: if (getMessageID() != null)
463: sb.append(separator).append("MessageID: ").append(
464: getMessageID());
465:
466: SOAPHeader soapHeader = getEnvelope().getHeader();
467: if (soapHeader != null) {
468: sb.append(separator).append("Headers : ");
469: for (Iterator iter = soapHeader.examineAllHeaderBlocks(); iter
470: .hasNext();) {
471: Object o = iter.next();
472: if (o instanceof SOAPHeaderBlock) {
473: SOAPHeaderBlock headerBlock = (SOAPHeaderBlock) o;
474: sb.append(separator).append(
475: headerBlock.getLocalName()).append(" : ")
476: .append(headerBlock.getText());
477: } else if (o instanceof OMElement) {
478: OMElement headerElem = (OMElement) o;
479: sb.append(separator).append(
480: headerElem.getLocalName()).append(" : ")
481: .append(headerElem.getText());
482: }
483: }
484: }
485:
486: return sb.toString();
487: }
488:
489: }
|