1 /*
2  * Copyright (C) 2018 The Android Open Source Project
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 com.android.server.wifi.hotspot2.soap;
18 
19 import android.annotation.NonNull;
20 import android.content.Context;
21 
22 import com.android.server.wifi.hotspot2.SystemInfo;
23 import com.android.server.wifi.hotspot2.omadm.DevDetailMo;
24 import com.android.server.wifi.hotspot2.omadm.DevInfoMo;
25 
26 import org.ksoap2.SoapEnvelope;
27 import org.ksoap2.serialization.SoapObject;
28 import org.ksoap2.serialization.SoapPrimitive;
29 import org.ksoap2.serialization.SoapSerializationEnvelope;
30 
31 /**
32  * This class represents sppPostDevData message, as part of the
33  * Subscription Provisioning Protocol.
34  * For the detail, refer to the Hotspot 2.0 rel2 specification.
35  */
36 public class PostDevDataMessage {
37 
38     /**
39      * Serialize the given request to a SOAP envelope.
40      *
41      * @param context       the instance of {@link Context}
42      * @param info          the instance of {@link SystemInfo} that has system information about
43      *                      the device.
44      * @param redirectUri   the redirect uri generated by the device and is used for indication
45      *                      of user input from the server.
46      * @param requestReason the reason that make a request to the server.
47      * @param sessionId     session id generated by the server to identify the session between
48      *                      device and server. It is <code>null</code> for the first request.
49      * @return {@link SoapSerializationEnvelope}
50      */
serializeToSoapEnvelope(@onNull Context context, @NonNull SystemInfo info, @NonNull String redirectUri, @NonNull String requestReason, String sessionId)51     public static SoapSerializationEnvelope serializeToSoapEnvelope(@NonNull Context context,
52             @NonNull SystemInfo info, @NonNull String redirectUri, @NonNull String requestReason,
53             String sessionId) {
54         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);
55         envelope.implicitTypes = true;       // Do not include type in element attribute
56         envelope.setAddAdornments(false);    // Do not generate/include IDs for each element
57 
58         SoapObject requestObject =
59                 new SoapObject(SoapEnvelope.NS20, SppConstants.METHOD_POST_DEV_DATA);
60         requestObject.addAttribute(SoapEnvelope.NS20, SppConstants.ATTRIBUTE_SPP_VERSION,
61                 SppConstants.SUPPORTED_SPP_VERSION);
62         requestObject.addAttribute(SppConstants.ATTRIBUTE_REQUEST_REASON, requestReason);
63 
64         requestObject.addAttribute(SppConstants.ATTRIBUTE_REDIRECT_URI, redirectUri);
65 
66         if (sessionId != null) {
67             requestObject.addAttribute(SoapEnvelope.NS20, SppConstants.ATTRIBUTE_SESSION_ID,
68                     sessionId);
69         }
70 
71         requestObject.addProperty(SoapEnvelope.NS20, SppConstants.PROPERTY_SUPPORTED_SPP_VERSIONS,
72                 SppConstants.SUPPORTED_SPP_VERSION);
73         requestObject.addProperty(SoapEnvelope.NS20, SppConstants.PROPERTY_SUPPORTED_MO_LIST,
74                 String.join(" ", SppConstants.SUPPORTED_MO_LIST));
75         addMoContainer(requestObject, DevInfoMo.URN, DevInfoMo.serializeToXml(info));
76         addMoContainer(requestObject, DevDetailMo.URN,
77                 DevDetailMo.serializeToXml(context, info, redirectUri));
78         envelope.setOutputSoapObject(requestObject);
79         return envelope;
80     }
81 
82     /**
83      * Add an Management Object (MO) tree to the given {@link SoapObject}.
84      *
85      * @param soapObject the object to add to
86      * @param moUrn the URN of the MO tree
87      * @param moText the string representation of the MO tree
88      */
addMoContainer(SoapObject soapObject, String moUrn, String moText)89     private static void addMoContainer(SoapObject soapObject, String moUrn, String moText) {
90         SoapPrimitive moContainer =
91                 new SoapPrimitive(SoapEnvelope.NS20, SppConstants.PROPERTY_MO_CONTAINER, moText);
92         moContainer.addAttribute(SoapEnvelope.NS20, SppConstants.ATTRIBUTE_MO_URN, moUrn);
93         soapObject.addProperty(SoapEnvelope.NS20, SppConstants.PROPERTY_MO_CONTAINER, moContainer);
94     }
95 }