View Javadoc

1   /*
2    * Copyright 2006 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.ws.context;
18  
19  import java.io.IOException;
20  import java.io.InputStream;
21  
22  import org.springframework.util.Assert;
23  import org.springframework.ws.WebServiceMessage;
24  import org.springframework.ws.WebServiceMessageFactory;
25  
26  /**
27   * Default implementation of <code>MessageContext</code>.
28   *
29   * @author Arjen Poutsma
30   * @since 1.0.0
31   */
32  public class DefaultMessageContext extends AbstractMessageContext {
33  
34      private final WebServiceMessageFactory messageFactory;
35  
36      private final WebServiceMessage request;
37  
38      private WebServiceMessage response;
39  
40      /** Construct a new, empty instance of the <code>DefaultMessageContext</code> with the given message factory. */
41      public DefaultMessageContext(WebServiceMessageFactory messageFactory) {
42          this(messageFactory.createWebServiceMessage(), messageFactory);
43      }
44  
45      /**
46       * Construct a new instance of the <code>DefaultMessageContext</code> with the given request message and message
47       * factory.
48       */
49      public DefaultMessageContext(WebServiceMessage request, WebServiceMessageFactory messageFactory) {
50          Assert.notNull(request, "request must not be null");
51          Assert.notNull(messageFactory, "messageFactory must not be null");
52          this.request = request;
53          this.messageFactory = messageFactory;
54      }
55  
56      public WebServiceMessage getRequest() {
57          return request;
58      }
59  
60      public boolean hasResponse() {
61          return response != null;
62      }
63  
64      public WebServiceMessage getResponse() {
65          if (response == null) {
66              response = messageFactory.createWebServiceMessage();
67          }
68          return response;
69      }
70  
71      public void setResponse(WebServiceMessage response) {
72          checkForResponse();
73          this.response = response;
74      }
75  
76      public void clearResponse() {
77          response = null;
78      }
79  
80      public void readResponse(InputStream inputStream) throws IOException {
81          checkForResponse();
82          response = messageFactory.createWebServiceMessage(inputStream);
83      }
84  
85      public WebServiceMessageFactory getMessageFactory() {
86          return messageFactory;
87      }
88  
89      private void checkForResponse() throws IllegalStateException {
90          if (response != null) {
91              throw new IllegalStateException("Response message already created");
92          }
93      }
94  }