• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Esmertec AG.
3  * Copyright (C) 2007 The Android Open Source Project
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package com.android.im.imps;
19 
20 /**
21  * A primitive is the basic packet sent between the IMPS server and the IMPS
22  * client. Note that this class is not thread-safe.
23  */
24 public final class Primitive {
25     private TransactionMode mTransactionMode = TransactionMode.Request;
26     private String mTransactionId;
27 
28     private String mSessionId;
29 
30     private String mPoll;
31     private String mCir;
32 
33     private PrimitiveElement mContentElement;
34 
35     /**
36      * Constructs a new Primitive with default value.
37      */
Primitive()38     public Primitive() {
39     }
40 
41     /**
42      * Constructs a new Primitive with a type.
43      *
44      * @param type the type of the primitive.
45      */
Primitive(String type)46     public Primitive(String type) {
47         mContentElement = new PrimitiveElement(type);
48     }
49 
50     /**
51      * Gets the session type of this primitive.
52      *
53      * @return the session type .
54      */
getSessionType()55     public SessionType getSessionType() {
56         return mSessionId == null ? SessionType.Outband : SessionType.Inband;
57     }
58 
59     /**
60      * Gets the session ID of this primitive.
61      *
62      * @return the session ID.
63      */
getSessionId()64     public String getSessionId() {
65         return mSessionId;
66     }
67 
68     /**
69      * Sets the session ID of this primitive.
70      *
71      * @param sessionId the session ID.
72      */
setSession(String sessionId)73     public void setSession(String sessionId) {
74         this.mSessionId = sessionId;
75     }
76 
77     /**
78      * Gets the transaction mode of this primitive.
79      *
80      * @return the transaction mode.
81      */
getTransactionMode()82     public TransactionMode getTransactionMode() {
83         return mTransactionMode;
84     }
85 
86     /**
87      * Sets the transaction mode of this primitive.
88      *
89      * @param mode the transaction mode.
90      */
setTransactionMode(TransactionMode mode)91     public void setTransactionMode(TransactionMode mode) {
92         this.mTransactionMode = mode;
93     }
94 
95     /**
96      * Gets the transaction ID of this primitive.
97      *
98      * @return the transaction ID.
99      */
getTransactionID()100     public String getTransactionID() {
101         return mTransactionId;
102     }
103 
104     /**
105      * Sets the transaction ID of this primitive.
106      * @param transId the transaction ID.
107      */
setTransactionId(String transId)108     public void setTransactionId(String transId) {
109         this.mTransactionId = transId;
110     }
111 
setTransaction(ImpsTransaction transaction)112     public void setTransaction(ImpsTransaction transaction) {
113         this.mTransactionId = transaction.getId();
114     }
115 
getCir()116     public String getCir() {
117         return mCir;
118     }
119 
setCir(String cir)120     public void setCir(String cir) {
121         this.mCir = cir;
122     }
123 
getPoll()124     public String getPoll() {
125         return mPoll;
126     }
127 
setPoll(String poll)128     public void setPoll(String poll) {
129         this.mPoll = poll;
130     }
131 
getType()132     public String getType() {
133         return (mContentElement == null) ? null : mContentElement.getTagName();
134     }
135 
getContentElement()136     public PrimitiveElement getContentElement() {
137         return mContentElement;
138     }
139 
setContentElement(String type)140     public void setContentElement(String type) {
141         mContentElement = new PrimitiveElement(type);
142     }
143 
addElement(String tag)144     public PrimitiveElement addElement(String tag) {
145         return mContentElement.addChild(tag);
146     }
147 
addElement(String tag, String value)148     public void addElement(String tag, String value) {
149         mContentElement.addChild(tag, value);
150     }
151 
addElement(String tag, boolean value)152     public void addElement(String tag, boolean value) {
153         mContentElement.addChild(tag, value);
154     }
155 
addElement(PrimitiveElement elem)156     public void addElement(PrimitiveElement elem) {
157         mContentElement.addChild(elem);
158     }
159 
getElement(String tag)160     public PrimitiveElement getElement(String tag) {
161         return mContentElement.getChild(tag);
162     }
163 
getElementContents(String tag)164     public String getElementContents(String tag) {
165         PrimitiveElement elem = getElement(tag);
166         return elem == null ? null : elem.getContents();
167     }
168 
createMessage(String versionUri, String transactUri)169     PrimitiveElement createMessage(String versionUri, String transactUri) {
170         PrimitiveElement root = new PrimitiveElement(ImpsTags.WV_CSP_Message);
171 
172         root.setAttribute(ImpsTags.XMLNS, versionUri);
173         PrimitiveElement sessionElem = root.addChild(ImpsTags.Session);
174 
175         PrimitiveElement sessionDescElem = sessionElem.addChild(
176                 ImpsTags.SessionDescriptor);
177         sessionDescElem.addChild(ImpsTags.SessionType,
178                 getSessionType().toString());
179         if (getSessionId() != null) {
180             sessionDescElem.addChild(ImpsTags.SessionID, getSessionId());
181         }
182 
183         PrimitiveElement transElem = sessionElem.addChild(ImpsTags.Transaction);
184 
185         PrimitiveElement transDescElem = transElem.addChild(
186                 ImpsTags.TransactionDescriptor);
187         transDescElem.addChild(ImpsTags.TransactionMode,
188                 getTransactionMode().toString());
189         if (getTransactionID() != null) {
190             transDescElem.addChild(ImpsTags.TransactionID, getTransactionID());
191         }
192 
193         PrimitiveElement transContentElem = transElem.addChild(
194                 ImpsTags.TransactionContent);
195         transContentElem.setAttribute(ImpsTags.XMLNS, transactUri);
196         transContentElem.addChild(getContentElement());
197 
198         return root;
199     }
200 
201     /**
202      * Represents the transaction mode of a primitive.
203      */
204     public static enum TransactionMode {
205         /**
206          * Indicates the primitive is a request in a transaction.
207          */
208         Request,
209 
210         /**
211          * Indicates the primitive is a response in a transaction.
212          */
213         Response
214     }
215 
216     /**
217      * Represents the session type of a primitive.
218      */
219     public static enum SessionType {
220         /**
221          * Indicates a primitive is sent within a session.
222          */
223         Inband,
224 
225         /**
226          * Indicates a primitive is sent without a session.
227          */
228         Outband
229     }
230 }
231