replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / service / easy-setup / mediator / richsdk / android / EasySetupCore / src / main / java / org / iotivity / service / easysetup / mediator / CloudProp.java
1 /**
2  * ***************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * ****************************************************************
19  */
20
21 package org.iotivity.service.easysetup.mediator;
22
23 import android.util.Log;
24
25 import org.iotivity.service.easysetup.mediator.enums.OAUTH_TOKENTYPE;
26
27 import org.iotivity.base.OcException;
28 import org.iotivity.base.OcRepresentation;
29
30 /**
31  * This class contains cloud server properties to be delivered to Enrollee
32  */
33 public class CloudProp {
34     private static final String TAG = CloudProp.class.getName();
35     protected OcRepresentation mRep = null;
36     protected String mCloudID = null;
37     protected int mCredID ;
38
39     /**
40      * Constructor
41      */
42     public CloudProp() {
43         mRep = new OcRepresentation();
44         mCloudID = "";
45     }
46
47     public void setCloudProp(String authCode, String authProvider, String ciServer)
48     {
49         if(authCode == null)
50         {
51             authCode = "";
52         }
53         if(authProvider == null)
54         {
55             authProvider = "";
56         }
57         if(ciServer == null)
58         {
59             ciServer = "";
60         }
61         try {
62             mRep.setValue(ESConstants.OC_RSRVD_ES_AUTHCODE, authCode);
63             mRep.setValue(ESConstants.OC_RSRVD_ES_AUTHPROVIDER, authProvider);
64             mRep.setValue(ESConstants.OC_RSRVD_ES_CISERVER, ciServer);
65         } catch (OcException e) {
66             Log.e(TAG, "setCloudProp is failed.");
67         }
68     }
69
70     public void setCloudPropWithAccessToken(String accessToken, OAUTH_TOKENTYPE tokenType,
71                                         String authProvider, String ciServer)
72     {
73         if(accessToken == null)
74         {
75             accessToken = "";
76         }
77         if(authProvider == null)
78         {
79             authProvider = "";
80         }
81         if(ciServer == null)
82         {
83             ciServer = "";
84         }
85         try {
86             mRep.setValue(ESConstants.OC_RSRVD_ES_ACCESSTOKEN, accessToken);
87             mRep.setValue(ESConstants.OC_RSRVD_ES_ACCESSTOKEN_TYPE, tokenType.getValue());
88             mRep.setValue(ESConstants.OC_RSRVD_ES_AUTHPROVIDER, authProvider);
89             mRep.setValue(ESConstants.OC_RSRVD_ES_CISERVER, ciServer);
90         } catch (OcException e) {
91             Log.e(TAG, "setCloudPropWithAccessToken is failed.");
92         }
93     }
94
95     public void setCloudID(String cloudID)
96     {
97         mCloudID = cloudID;
98     }
99
100     public void setCredID(int credID)
101     {
102         mCredID = credID;
103     }
104     /**
105      * This method returns the authCode used for the first registration to IoTivity cloud
106      * @return AuthCode for sign-up to IoTivity cloud
107      */
108     public String getAuthCode()
109     {
110         if(mRep == null)
111         {
112             return null;
113         }
114
115         try
116         {
117             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_AUTHCODE))
118                 return mRep.getValue(ESConstants.OC_RSRVD_ES_AUTHCODE);
119         }
120         catch (OcException e)
121         {
122             Log.e(TAG, "getAuthCode is failed.");
123         }
124         return null;
125     }
126
127     /**
128      * This method returns the auth provider which issued the given AuthCode
129      * @return Auth provider which issued the given AuthCode
130      */
131     public String getAuthProvider()
132     {
133         if(mRep == null)
134         {
135             return null;
136         }
137
138         try
139         {
140             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_AUTHPROVIDER))
141                 return mRep.getValue(ESConstants.OC_RSRVD_ES_AUTHPROVIDER);
142         }
143         catch (OcException e)
144         {
145             Log.e(TAG, "getAuthProvider is failed.");
146         }
147         return null;
148     }
149
150         /**
151      * This method returns the Cloud Interface server's URL to be registered
152      * @return CI server's URL to be registered
153      */
154     public String getCiServer()
155     {
156         if(mRep == null)
157         {
158             return null;
159         }
160
161         try
162         {
163             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_CISERVER))
164                 return mRep.getValue(ESConstants.OC_RSRVD_ES_CISERVER);
165         }
166         catch (OcException e)
167         {
168             Log.e(TAG, "getCiServer is failed.");
169         }
170         return null;
171     }
172
173     /**
174      * This method returns the Cloud Interface server's UUID
175      * @return CI server's UUID
176      */
177     public String getCloudID()
178     {
179         return mCloudID;
180     }
181
182     /**
183      * This method returns the Cloud Certificate's Cred ID
184      * @return CI server's Credential ID of Certificate
185      */
186     public int getCredID()
187     {
188         return mCredID;
189     }
190
191     /**
192      * This method returns an accessToken used for the first registration to IoTivity cloud
193      * @return accessToken for sign-up to IoTivity cloud
194      */
195     public String getAccessToken()
196     {
197         if(mRep == null)
198         {
199             return null;
200         }
201
202         try
203         {
204             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_ACCESSTOKEN))
205                 return mRep.getValue(ESConstants.OC_RSRVD_ES_ACCESSTOKEN);
206         }
207         catch (OcException e)
208         {
209             Log.e(TAG, "getAccessToken is failed.");
210         }
211         return null;
212     }
213
214     /**
215      * This method returns an access token type
216      * @return tokenType of access token
217      */
218     public OAUTH_TOKENTYPE getAccessTokenType()
219     {
220         if(mRep == null)
221         {
222             return null;
223         }
224
225         try
226         {
227             if (mRep.hasAttribute(ESConstants.OC_RSRVD_ES_ACCESSTOKEN_TYPE))
228                 return OAUTH_TOKENTYPE.fromInt((int)mRep.getValue(ESConstants.OC_RSRVD_ES_ACCESSTOKEN_TYPE));
229         }
230         catch (OcException e)
231         {
232             Log.e(TAG, "getAccessTokenType is failed.");
233         }
234         return OAUTH_TOKENTYPE.NONE_OAUTH_TOKENTYPE;
235     }
236
237     public OcRepresentation toOCRepresentation()
238     {
239         return mRep;
240     }
241 }