javauis/m2g_qt/javasrc/com/nokia/microedition/m2g/connection/file/M2GConnectionPolicyImpl.java
changeset 80 d6dafc5d983f
parent 56 abc41079b313
child 87 1627c337e51e
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*
       
     2 * Copyright (c) 2005 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 package com.nokia.microedition.m2g.connection.file;
       
    19 
       
    20 import java.io.IOException;
       
    21 import java.lang.NullPointerException;
       
    22 import java.lang.IllegalArgumentException;
       
    23 import java.lang.SecurityException;
       
    24 import com.nokia.microedition.m2g.connection.*;
       
    25 import com.nokia.microedition.m2g.M2GSVGConstants;
       
    26 import com.nokia.mj.impl.utils.Logger;
       
    27 
       
    28 /**
       
    29  * Connection policy
       
    30  * <b>DRM - DECRYPTED MODE</b>
       
    31  * <p>
       
    32  * <b>Description</b>
       
    33  * <br>
       
    34  * When a MIDlet tries to open a file using a URL that starts with file:// and
       
    35  * ends either with ?drm=dec+display or with ?drm=dec+display+preview,
       
    36  * this is interpreted as a request to open the file for reading of the
       
    37  * content in the plaintext form.
       
    38  * Example URLs:
       
    39  * file:///E:/foo.svg?drm=dec+display
       
    40  * file:///E:/foo.svg?drm=dec+display+preview
       
    41  * <br>
       
    42  * Supported only if the midlet belongs to MANUFACTURER DOMAIN otherwise
       
    43  * java.lang.SecurityException is thrown.
       
    44  * </p>
       
    45  * <br>
       
    46  * <br>
       
    47  * <b>DRM - ENCRYPTED MODE</b>
       
    48  * <p>
       
    49  * <b>Description</b>
       
    50  * <br>
       
    51  * When a MIDlet tries to open a file using a URL that starts with file:// and
       
    52  * ends with ?drm=enc, this is interpreted as a request to open the
       
    53  * file for reading of the content in the encrypted form.
       
    54  * Example URLs:
       
    55  * file:///E:/foo.svg?drm=enc
       
    56  * <br>
       
    57  * Not supported, java.lang.IllegalArgumentException is thrown always.
       
    58  * </p>
       
    59  * <br>
       
    60  * <br>
       
    61  * <b>DRM - DEFAULT MODE</b>
       
    62  * <p>
       
    63  * <b>Description</b>
       
    64  * <br>
       
    65  * When a MIDlet tries to open a file using either a normal file URL or a
       
    66  * file URL that ends with ?drm=preview, this is interpreted as a request to open the
       
    67  * file for reading of the content in the decrypted form.
       
    68  * NOTE: Preview mechanism is not available to MIDlets that don't
       
    69  * belong to MANUFACTURER DOMAIN.
       
    70  * Example URLs:
       
    71  * file:///E:/foo.svg
       
    72  * file:///E:/foo.svg?drm=preview
       
    73  * <br>
       
    74  * Access to SVG document via DOM API is NOT allowed, i.e. only rendering of the
       
    75  * content is allowed if a MIDlet uses a default mode URL.
       
    76  * If it's tryed to access to SVG document via DOM API then
       
    77  * java.lang.SecurityException is thrown.
       
    78  * </p>
       
    79  */
       
    80 public class M2GConnectionPolicyImpl implements M2GConnectionPolicy
       
    81 {
       
    82     //--------------------------------------------------
       
    83     // STATIC CONSTANTS
       
    84     //--------------------------------------------------
       
    85     public static final String ENC_NOT_SUPPORTED_ESTR =
       
    86         "A URL with an encrypted mode is not supported.";
       
    87     public static final String DRM_PARAMETER =
       
    88         "drm";
       
    89     public static final String DECRYPTED_MODE =
       
    90         "dec";
       
    91     public static final String ENCRYPTED_MODE =
       
    92         "enc";
       
    93     public static final String DEFAULT_MODE =
       
    94         "def";
       
    95     public static final String DEFAULT_PREVIEW_MODE =
       
    96         "def_preview";
       
    97     public static final String DRM_PREVIEW_ATTRIBUTE =
       
    98         "preview";
       
    99     public static final String DRM_DEFAULT_VALUES =
       
   100         "?drm=dec+display";
       
   101     public static final String DRM_DEFAULT_VALUES_AND_PREVIEW =
       
   102         "?drm=dec+display+preview";
       
   103     public static final String DRM_FILE_SUFFIX =
       
   104         "dcf";
       
   105 
       
   106     //--------------------------------------------------
       
   107     // VARIABLES
       
   108     //--------------------------------------------------
       
   109     private M2GUrlTokenizer iTokenizer;
       
   110     private boolean iAccessRight = true;
       
   111     private String iMode = null;
       
   112 
       
   113     //--------------------------------------------------
       
   114     // METHODS
       
   115     //--------------------------------------------------
       
   116     /**
       
   117      * Ctor
       
   118      */
       
   119     public M2GConnectionPolicyImpl(M2GUrlTokenizer aTokenizer)
       
   120     {
       
   121         iTokenizer = aTokenizer;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Check domain
       
   126      * @throws SecurityException if not access rights
       
   127      */
       
   128     public void checkDomain(boolean isManufacturerDomain) throws SecurityException
       
   129     {
       
   130         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   131                    "checkDomain(): manufacturer domain=" + isManufacturerDomain
       
   132                    + ", mode=" + iMode + ", access right=" + iAccessRight + " - begin");
       
   133 
       
   134         // If drm mode then
       
   135         if (iMode != null)
       
   136         {
       
   137             // If dec mode and not manufacturer domain
       
   138             if (iMode.equals(DECRYPTED_MODE))
       
   139             {
       
   140                 if (!isManufacturerDomain)
       
   141                 {
       
   142                     Logger.ELOG(Logger.EJavaUI,
       
   143                                 "checkDomain() - Not manufacturer and dec: SecurityException");
       
   144                     throw new SecurityException(M2GSVGConstants.ACCESS_RIGHTS_ESTR);
       
   145                 }
       
   146             }
       
   147             // If default + preview mode
       
   148             else if (iMode.equals(DEFAULT_PREVIEW_MODE))
       
   149             {
       
   150                 if (!isManufacturerDomain)
       
   151                 {
       
   152                     Logger.ELOG(Logger.EJavaUI,
       
   153                                 "checkDomain() - Not manufacturer and def+preview: SecurityException");
       
   154                     throw new SecurityException(M2GSVGConstants.ACCESS_RIGHTS_ESTR);
       
   155                 }
       
   156             }
       
   157             // If not default mode
       
   158             else if (!iMode.equals(DEFAULT_MODE))
       
   159             {
       
   160                 Logger.ELOG(Logger.EJavaUI,
       
   161                             "checkDomain() - Not def nor dec: SecurityException");
       
   162                 throw new SecurityException(M2GSVGConstants.ACCESS_RIGHTS_ESTR);
       
   163             }
       
   164         }
       
   165     }
       
   166 
       
   167     /**
       
   168      * Check protection type
       
   169      * @param aType If type is null then file is not drm protected.
       
   170      */
       
   171     public void checkProtectionType(String aType)
       
   172     {
       
   173         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   174                    "checkProtectionType() type=" + aType + ", mode=" + iMode +
       
   175                    ", access right=" + iAccessRight + " - begin");
       
   176 
       
   177         if ((aType != null) && (!aType.equals("")))
       
   178         {
       
   179             if (iMode == null)
       
   180             {
       
   181                 iMode = DEFAULT_MODE;
       
   182                 iAccessRight = false;
       
   183             }
       
   184         }
       
   185 
       
   186         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   187                    "checkProtectionType() mode=" + iMode + ", access right=" +
       
   188                    iAccessRight + " - end");
       
   189     }
       
   190 
       
   191     /**
       
   192        * Get access rights
       
   193        * @see M2GConnectionPolicy#getAccessRight()
       
   194        */
       
   195     public boolean getAccessRight()
       
   196     {
       
   197         return iAccessRight;
       
   198     }
       
   199 
       
   200     /**
       
   201        * Initialize
       
   202        * @param aTokenizer Tokenizer
       
   203        * @throws IllegalArgumentException if drm with encrypted mode
       
   204      */
       
   205     public void initialize(M2GUrlTokenizer aTokenizer)
       
   206     {
       
   207         iAccessRight = true;
       
   208         iMode = null; // no DRM at all
       
   209         iTokenizer = aTokenizer;
       
   210         String parameters = iTokenizer.getParameters();
       
   211         int pos = -1;
       
   212         // Check if a url is something more than just a normal url.
       
   213         // Is the drm parameter within the parameters?
       
   214         if ((parameters != null) && (pos = parameters.indexOf(DRM_PARAMETER)) != 1)
       
   215         {
       
   216             pos += DRM_PARAMETER.length();
       
   217             // Encrypted mode?
       
   218             if (parameters.indexOf(ENCRYPTED_MODE, pos) != -1)
       
   219             {
       
   220                 Logger.ELOG(Logger.EJavaUI, "initialize(): drm - encrypted mode");
       
   221                 iAccessRight = false;
       
   222                 iMode = ENCRYPTED_MODE;
       
   223                 throw new IllegalArgumentException(ENC_NOT_SUPPORTED_ESTR);
       
   224             }
       
   225             // Decrypted mode?
       
   226             else if (parameters.indexOf(DECRYPTED_MODE, pos) != -1)
       
   227             {
       
   228                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   229                            "initialize(): drm - decrypted mode");
       
   230                 iAccessRight = true;
       
   231                 iMode = DECRYPTED_MODE;
       
   232             }
       
   233             // Default + preview mode?
       
   234             else if (parameters.indexOf(DRM_PREVIEW_ATTRIBUTE, pos) != -1)
       
   235             {
       
   236                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   237                            "initialize(): drm - decrypted mode");
       
   238                 iAccessRight = false;
       
   239                 iMode = DEFAULT_PREVIEW_MODE;
       
   240             }
       
   241             // Default mode
       
   242             else
       
   243             {
       
   244                 Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   245                            "initialize(): drm - default mode");
       
   246                 iAccessRight = false;
       
   247                 iMode = DEFAULT_MODE;
       
   248             }
       
   249         }
       
   250         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   251                    "initialize() mode=" + iMode + ", access right="
       
   252                    + iAccessRight + " - end");
       
   253     }
       
   254 
       
   255     /**
       
   256      * Get drm mode
       
   257      * @return mode. Null if no drm
       
   258      */
       
   259     public String getMode()
       
   260     {
       
   261         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   262                    "getMode(): " + iMode + "- begin");
       
   263         return iMode;
       
   264     }
       
   265 
       
   266     /**
       
   267      * Get url
       
   268      * @return File url
       
   269      */
       
   270     public String getUrl()
       
   271     {
       
   272         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   273                    "getUrl() mode=" + iMode + ", access right=" + iAccessRight + " - begin");
       
   274 
       
   275         String url = iTokenizer.getUrl();
       
   276         String parameters = iTokenizer.getParameters();
       
   277         // If normal url or default drm url
       
   278         if ((iMode == null) ||
       
   279                 (iMode.equals(DEFAULT_MODE)) ||
       
   280                 (iMode.equals(DEFAULT_PREVIEW_MODE)))
       
   281         {
       
   282             Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   283                        "getUrl() parse default url");
       
   284             // Url with preview attribute?
       
   285             if ((parameters != null) &&
       
   286                     (parameters.indexOf(DRM_PREVIEW_ATTRIBUTE) != -1))
       
   287             {
       
   288                 url = (iTokenizer.getBasePart() + DRM_DEFAULT_VALUES_AND_PREVIEW);
       
   289             }
       
   290             // Normal file url?
       
   291             else
       
   292             {
       
   293                 // File API is able to open normal file without drm protection even
       
   294                 // when the url contains the drm parameter
       
   295                 url = (iTokenizer.getBasePart() + DRM_DEFAULT_VALUES);
       
   296             }
       
   297         }
       
   298         Logger.LOG(Logger.EJavaUI, Logger.EInfo,
       
   299                    "getUrl(): " + url + " - end");
       
   300         return url;
       
   301     }
       
   302 }