View Javadoc

1   /*
2   Copyright 2011-2014 AGH-UST, http://www.agh.edu.pl
3   Faculty of Computer Science, Electronics and Telecommunications
4   Department of Computer Science 
5   
6   See the NOTICE file distributed with this work for additional
7   information regarding copyright ownership
8   
9   Licensed under the Apache License, Version 2.0 (the "License");
10  you may not use this file except in compliance with the License.
11  You may obtain a copy of the License at
12  
13    http://www.apache.org/licenses/LICENSE-2.0
14  
15  Unless required by applicable law or agreed to in writing, software
16  distributed under the License is distributed on an "AS IS" BASIS,
17  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  See the License for the specific language governing permissions and
19  limitations under the License.
20  */
21  package org.universAAL.ri.gateway.communicator.service.impl;
22  
23  import java.io.Serializable;
24  import java.net.URL;
25  import java.util.UUID;
26  
27  import org.universAAL.ri.gateway.communicator.service.Message;
28  
29  /**
30   * A message wrapper for requests and responses sent between communicators.
31   * 
32   * @author skallz
33   * 
34   */
35  public class MessageWrapper implements Serializable {
36  
37      /**
38       * 
39       */
40      private static final long serialVersionUID = 900568682837234611L;
41  
42      /**
43       * An ID to identify during asynchronous communication.
44       */
45      private UUID id;
46      /**
47       * Type of the message.
48       */
49      private MessageType type;
50      /**
51       * The message.
52       */
53      private Message message;
54      /**
55       * where to send back the response (for asynchronous communication only).
56       */
57      private URL returnTo;
58      
59      private String sourceId;
60      
61      /**
62       * Wraps a message with a concrete ID (for responses).
63       * 
64       * @param type
65       *            type of the message
66       * @param message
67       *            the message
68       * @param id
69       *            ID
70       */
71      public MessageWrapper(final MessageType type, final Message message,
72  	    final UUID id, final String sourceId) {
73  	this.type = type;
74  	this.message = message;
75  	this.id = id;
76  	this.sourceId = sourceId;
77      }
78  
79      /**
80       * Wraps a message with a random ID.
81       * 
82       * @param type
83       *            type of the message
84       * @param message
85       *            the message
86       */
87      public MessageWrapper(final MessageType type, final Message message,
88  	    final String sourceId) {
89  	this(type, message, UUID.randomUUID(), sourceId);
90      }
91  
92      /**
93       * Wraps an asynchronous request stating where it should be sent back.
94       * 
95       * @param type
96       *            type of the message
97       * @param message
98       *            the message
99       * @param returnTo
100      *            where to send it back
101      */
102     public MessageWrapper(final MessageType type, final Message message,
103 	    final URL returnTo, final String sourceId) {
104 	this(type, message, sourceId);
105 	this.returnTo = returnTo;
106     }
107 
108     @Override
109     public String toString() {
110 	return String.format("msg:%s id:%s", message, id);
111     }
112 
113     /**
114      * @return the id
115      */
116     public UUID getId() {
117 	return id;
118     }
119 
120     /**
121      * @return the type
122      */
123     public MessageType getType() {
124 	return type;
125     }
126 
127     /**
128      * @return the message
129      */
130     public Message getMessage() {
131 	return message;
132     }
133 
134     /**
135      * @return the returnTo
136      */
137     public URL getReturnTo() {
138 	return returnTo;
139     }
140 
141     public String getSourceId() {
142 	return sourceId;
143     }
144 
145     public void setSourceId(final String sourceId) {
146 	this.sourceId = sourceId;
147     }
148 
149 }