1 package com.android.hotspot2.osu; 2 3 import com.android.hotspot2.omadm.OMAConstants; 4 import com.android.hotspot2.omadm.OMAException; 5 import com.android.hotspot2.omadm.XMLNode; 6 7 import java.util.HashMap; 8 import java.util.Map; 9 10 public abstract class OSUResponse { 11 private static final String SPPVersionAttribute = "sppVersion"; 12 private static final String SPPStatusAttribute = "sppStatus"; 13 private static final String SPPSessionIDAttribute = "sessionID"; 14 15 private final OSUMessageType mMessageType; 16 private final String mVersion; 17 private final String mSessionID; 18 private final OSUStatus mStatus; 19 private final OSUError mError; 20 private final Map<String, String> mAttributes; 21 OSUResponse(XMLNode root, OSUMessageType messageType, String... attributes)22 protected OSUResponse(XMLNode root, OSUMessageType messageType, String... attributes) 23 throws OMAException { 24 mMessageType = messageType; 25 String ns = root.getNameSpace() + ":"; 26 mVersion = root.getAttributeValue(ns + SPPVersionAttribute); 27 mSessionID = root.getAttributeValue(ns + SPPSessionIDAttribute); 28 29 String status = root.getAttributeValue(ns + SPPStatusAttribute); 30 if (status == null) { 31 throw new OMAException("Missing status"); 32 } 33 mStatus = OMAConstants.mapStatus(status); 34 35 if (mVersion == null || mSessionID == null || mStatus == null) { 36 throw new OMAException("Incomplete request: " + root.getAttributes()); 37 } 38 39 if (attributes != null) { 40 mAttributes = new HashMap<>(); 41 for (String attribute : attributes) { 42 String value = root.getAttributeValue(ns + attribute); 43 if (value == null) { 44 throw new OMAException("Missing attribute: " + attribute); 45 } 46 mAttributes.put(attribute, value); 47 } 48 } else { 49 mAttributes = null; 50 } 51 52 if (mStatus == OSUStatus.Error) { 53 OSUError error = null; 54 String errorTag = ns + "sppError"; 55 for (XMLNode child : root.getChildren()) { 56 if (child.getTag().equals(errorTag)) { 57 error = OMAConstants.mapError(child.getAttributeValue("errorCode")); 58 break; 59 } 60 } 61 mError = error; 62 } else { 63 mError = null; 64 } 65 } 66 getMessageType()67 public OSUMessageType getMessageType() { 68 return mMessageType; 69 } 70 getVersion()71 public String getVersion() { 72 return mVersion; 73 } 74 getSessionID()75 public String getSessionID() { 76 return mSessionID; 77 } 78 getStatus()79 public OSUStatus getStatus() { 80 return mStatus; 81 } 82 getError()83 public OSUError getError() { 84 return mError; 85 } 86 getAttributes()87 protected Map<String, String> getAttributes() { 88 return mAttributes; 89 } 90 91 @Override toString()92 public String toString() { 93 return String.format("%s version '%s', status %s, session-id '%s'%s", 94 mMessageType, mVersion, mStatus, mSessionID, mError != null 95 ? (" (" + mError + ")") : ""); 96 } 97 } 98