Home | History | Annotate | Download | only in imps
      1 /*
      2  * Copyright (C) 2007-2008 Esmertec AG.
      3  * Copyright (C) 2007-2008 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 import java.util.Collections;
     21 import java.util.HashMap;
     22 import java.util.Map;
     23 import java.util.Random;
     24 
     25 import com.android.im.engine.ImErrorInfo;
     26 
     27 public class ImpsUtils {
     28 
     29     private static final HashMap<String, String> sClientInfo;
     30     private static String sSessionCookie;
     31     private static int sSessionCookieNumber;
     32 
     33     private ImpsUtils() {
     34     }
     35 
     36     static {
     37         // TODO: v1.2 doesn't support ClientContentLimit
     38         sClientInfo = new HashMap<String, String>();
     39         sClientInfo.put(ImpsTags.ClientType, ImpsClientCapability.getClientType());
     40         sClientInfo.put(ImpsTags.ClientProducer, ImpsConstants.CLIENT_PRODUCER);
     41         sClientInfo.put(ImpsTags.ClientVersion, ImpsConstants.CLIENT_VERSION);
     42     }
     43 
     44     /**
     45      * Checks if a string is a boolean value of true IMPS.
     46      *
     47      * @param value the string value.
     48      * @return <code>true</code> if it's true in IMPS.
     49      */
     50     public static boolean isTrue(String value) {
     51         return ImpsConstants.TRUE.equalsIgnoreCase(value);
     52     }
     53 
     54     /**
     55      * Checks if a string is a boolean value of false in IMPS.
     56      *
     57      * @param value the string value.
     58      * @return true if it's false in IMPS
     59      */
     60     public static boolean isFalse(String value) {
     61         return ImpsConstants.FALSE.equalsIgnoreCase(value);
     62     }
     63 
     64     /**
     65      * Return the IMPS String presentation of the boolean value
     66      *
     67      * @param isTrue the boolean value
     68      * @return the String presentation
     69      */
     70     public static String toImpsBool(boolean isTrue) {
     71         if (isTrue) {
     72             return ImpsConstants.TRUE;
     73         }
     74 
     75         return ImpsConstants.FALSE;
     76     }
     77 
     78     /**
     79      * Checks if the response primitive indicates successful.
     80      *
     81      * @param response the response primitive.
     82      * @returns <code>null</code> if the status code is 200 or an ImpsErrorInfo instance
     83      */
     84     public static ImpsErrorInfo checkResultError(Primitive response) {
     85         PrimitiveElement result = response.getElement(ImpsTags.Result);
     86         if (result == null) {
     87             return null;
     88         }
     89 
     90         String resultCode = result.getChild(ImpsTags.Code).getContents();
     91         if (!ImpsConstants.SUCCESS_CODE.equals(resultCode)) {
     92             PrimitiveElement descElem = result.getChild(ImpsTags.Description);
     93             String errorDesc = (descElem == null) ? "" : descElem.getContents();
     94             int statusCode = parseInt(resultCode, ImErrorInfo.ILLEGAL_SERVER_RESPONSE);
     95             return new ImpsErrorInfo(statusCode, errorDesc, response);
     96         }
     97         return null;
     98     }
     99 
    100     /**
    101      * Returns a copy of the string, with leading and trailing whitespace
    102      * omitted. Unlike the standard trim which just removes '\u0020'(the space
    103      * character), it removes all possible leading and trailing whitespace
    104      * character.
    105      *
    106      * @param str the string.
    107      * @return a copy of the string, with leading and trailing whitespace
    108      *         omitted.
    109      */
    110     public static String trim(String str) {
    111         if (null == str || "".equals(str))
    112             return str;
    113 
    114         int strLen = str.length();
    115         int start = 0;
    116         while (start < strLen && Character.isWhitespace(str.charAt(start)))
    117             start++;
    118         int end = strLen - 1;
    119         while (end >= 0 && Character.isWhitespace(str.charAt(end)))
    120             end--;
    121         if (end < start)
    122             return "";
    123         str = str.substring(start, end + 1);
    124         return str;
    125     }
    126 
    127     /**
    128      * Check whether the presence element has a qualified attribute value.
    129      * An attribute value is invalid when:
    130      *  1. An attribute is authorized but not yet updated for the first time
    131      *  2. The user wants to indicate that the value of the attribute is unknown.
    132      *
    133      * @param elem the presence element
    134      * @return <code>true</code> if the value of attribute is valid.
    135      */
    136     public static boolean isQualifiedPresence(PrimitiveElement elem) {
    137         if (null == elem || null == elem.getChild(ImpsTags.Qualifier)) {
    138             return false;
    139         }
    140 
    141         return ImpsUtils.isTrue(elem.getChildContents(ImpsTags.Qualifier));
    142     }
    143 
    144     public static Map<String, String> getClientInfo() {
    145         return Collections.unmodifiableMap(sClientInfo);
    146     }
    147 
    148     synchronized static String genSessionCookie() {
    149         if(sSessionCookie == null) {
    150             Random random = new Random();
    151             sSessionCookie = System.currentTimeMillis() + "" + random.nextInt();
    152         }
    153         return sSessionCookie + (sSessionCookieNumber++);
    154     }
    155 
    156     public static int parseInt(String s, int defaultValue) {
    157         try {
    158             return Integer.parseInt(s);
    159         } catch (NumberFormatException e) {
    160             // ignore
    161             return defaultValue;
    162         }
    163     }
    164 
    165     public static long parseLong(String s, long defaultValue) {
    166         try {
    167             return Long.parseLong(s);
    168         } catch (NumberFormatException e) {
    169             // ignore
    170             return defaultValue;
    171         }
    172     }
    173 }
    174