home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Security / custom / SamplePermission.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  11.0 KB  |  383 lines

  1. // SamplePermission.java
  2. //
  3. //
  4. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5. //
  6.  
  7. package com.ms.samples.security.custom;
  8.  
  9. import com.ms.security.*;
  10. import java.io.*;
  11. import com.ms.util.ini.*;
  12.  
  13.  
  14. /**
  15.  * Determines access to a list of items identified by strings.
  16.  */
  17. public class SamplePermission implements IPermission,
  18.                                          IEncodablePermission
  19. {
  20.     String[] items;
  21.     
  22.     public SamplePermission()
  23.     {
  24.     }
  25.  
  26.  
  27.     // Defines the PermissionID for this permission object.
  28.  
  29.     public static final PermissionID id = PolicyEngine.permissionNameToID(SamplePermission.class.getName());
  30.  
  31.  
  32.     // Methods to manipulate and inspect the permission
  33.  
  34.     /**
  35.      * Adds an allowed item to the permission.
  36.      */
  37.     public void addItem (String newitem)
  38.     {
  39.         if (items == null)
  40.         {
  41.             items = new String[1];
  42.         }
  43.         else
  44.         {
  45.             String[] newitems = new String[items.length+1];
  46.             System.arraycopy(items, 0, newitems, 0, items.length);
  47.             items = newitems;
  48.         }
  49.         items[items.length-1] = newitem;
  50.     }
  51.  
  52.     /**
  53.      * Removes an item that may (or not) already be allowed by the permission.
  54.      * @return true if the item existed before all occurrences were removed
  55.      */
  56.     public boolean removeItem (String remitem)
  57.     {
  58.         int nremoved = 0;
  59.         for (int i = 0; i < items.length; i++)
  60.         {
  61.             if (items[i] == remitem)
  62.             {
  63.                 items[i] = null;
  64.                 nremoved++;
  65.             }
  66.         }
  67.         
  68.         if (nremoved == 0)
  69.             return false;
  70.  
  71.         if (items.length == nremoved)
  72.         {
  73.             items = null;
  74.             return true;
  75.         }
  76.         
  77.         String[] newitems = new String[items.length-nremoved];
  78.         int ni = 0;
  79.         for (int i = 0; i < items.length; i++)
  80.         {
  81.             if (items[i] != null)
  82.                 newitems[ni++] = items[i];
  83.         }
  84.         items = newitems;
  85.  
  86.         return true;
  87.     }
  88.  
  89.     /**
  90.      * Obtains the items that are permitted.  The list should <em>not</em> be
  91.      * modified.
  92.      * @return the permitted items, or null if there are none.
  93.      */
  94.     public String[] getAllowedItems ()
  95.     {
  96.         // Permissions of classes are only accessible to fully trusted
  97.         // classes, so returning a reference to internal structures of
  98.         // the permission is acceptable.
  99.         return items;
  100.     }
  101.  
  102.  
  103.     // IEncodablePermission methods
  104.  
  105.     private void encodeText (PrintWriter w)
  106.     {
  107.         if (items == null)
  108.         {
  109.             w.println("empty");
  110.         }
  111.         else
  112.         {
  113.             w.println(items.length+" items:");
  114.             for (int i = 0; i < items.length; i++)
  115.             {
  116.                 w.println(items[i]);
  117.             }
  118.         }
  119.     }
  120.  
  121.     static final String TAG_TEXT1 = "String Version 1";
  122.     static final String TAG_INI1 = "INI Version 1";
  123.  
  124.     public boolean encode (String tag, OutputStream data)
  125.     {
  126.         try
  127.         {
  128.             DataOutputStream out = new DataOutputStream(data);
  129.  
  130.             // The format of the expected output is very rigid:
  131.             // DISPLAY, DISPLAYNAME: unicode characters
  132.             // TEXT: U2-length-prefixed utf8 string
  133.             // INI: an IniSection written to a PrintStream wrapping 'data'
  134.  
  135.             if (TAG_INI1.equals(tag))
  136.             {
  137.                 IniSection section = new IniSection();
  138.                 section.setName(this.getClass().getName());
  139.  
  140.                 if (items != null && items.length > 0)
  141.                 {
  142.                     StringBuffer sb = new StringBuffer();
  143.                     int i = 0;
  144.                     while (true)
  145.                     {
  146.                         sb.append(items[i]);
  147.                         i++;
  148.                         if (i >= items.length)
  149.                             break;
  150.                         sb.append(';');
  151.                     }
  152.                     section.addNamePair("Items", sb.toString());
  153.                 }
  154.  
  155.                 PrintStream ps = new PrintStream(data);
  156.                 section.writeContents(ps);
  157.  
  158.                 return !ps.checkError();
  159.             }
  160.             
  161.             if (EncodeFormats.DISPLAYNAME.equals(tag))
  162.             {
  163.                 out.writeChars("Sample Permission");
  164.                 return true;
  165.             }
  166.  
  167.             StringWriter sw = new StringWriter();
  168.             PrintWriter w = new PrintWriter(sw);
  169.             boolean needutf = false;
  170.             
  171.             if (TAG_TEXT1.equals(tag))
  172.             {
  173.                 needutf = true;
  174.             }
  175.             else if (EncodeFormats.DISPLAY.equals(tag))
  176.             {
  177.                 w.println("Sample Permission:");
  178.             }
  179.             else
  180.             {
  181.                 return false;
  182.             }
  183.             
  184.             encodeText(w);
  185.             
  186.             w.flush();
  187.             if (w.checkError())
  188.                 return false;
  189.  
  190.             String s = sw.toString();
  191.             
  192.             if (needutf)
  193.                 out.writeUTF(s);
  194.             else
  195.                 out.writeChars(s);
  196.             return true;
  197.         }
  198.         catch (IOException e)
  199.         {
  200.             return false;
  201.         }
  202.     }
  203.  
  204.     public boolean decode (String tag, InputStream data)
  205.     {
  206.         // See encode for the format of the input data
  207.  
  208.         if (TAG_TEXT1.equals(tag))
  209.         {
  210.             try
  211.             {
  212.                 BufferedReader r = new BufferedReader(new StringReader((new DataInputStream(data)).readUTF()));
  213.                 String line = r.readLine();
  214.                 
  215.                 if (line.equals("empty"))
  216.                 {
  217.                     items = null;
  218.                     return true;
  219.                 }
  220.                 
  221.                 int sep = line.indexOf(' ');
  222.                 if (sep == -1)
  223.                     return false;
  224.                     
  225.                 int nitems = Integer.parseInt(line.substring(0, sep));
  226.                 
  227.                 String[] newitems = new String[nitems];
  228.                 for (int i = 0; i < newitems.length; i++)
  229.                     newitems[i] = r.readLine();
  230.                     
  231.                 items = newitems;
  232.                 return true;
  233.             }
  234.             catch (IOException e)
  235.             {
  236.             }
  237.         }
  238.         else if (TAG_INI1.equals(tag))
  239.         {
  240.             IniFile ini;
  241.             try
  242.             {
  243.                 ini = new IniFile(data);
  244.             }
  245.             catch (Exception e)
  246.             {
  247.                 return false;
  248.             }
  249.  
  250.             IniSection section = ini.getSection(this.getClass().getName());
  251.             if (section != null)
  252.             {
  253.                 String val = section.getValue("Items");
  254.                 items = PermissionUtils.parseStringIntoArray(val, ";");
  255.                 return true;
  256.             }
  257.         }
  258.         
  259.         return false;
  260.     }
  261.         
  262.     public String mapFormat (String format)
  263.     {
  264.         if (format == null)
  265.         {
  266.             // The default format.  All permissions that wish to be encodable
  267.             // into signatures should support this format.
  268.             return TAG_TEXT1;
  269.         }
  270.         
  271.         if (EncodeFormats.TEXT.equals(format))
  272.             return TAG_TEXT1;
  273.             
  274.         if (EncodeFormats.INI.equals(format))
  275.             return TAG_INI1;
  276.  
  277.         // The tag is an opaque value used internally by the permission.  These
  278.         // formats are supported in encodings only, so version information in
  279.         // the tag is not important.
  280.         if (EncodeFormats.DISPLAYNAME.equals(format) || EncodeFormats.DISPLAY.equals(format))
  281.             return format;
  282.             
  283.         return null;
  284.     }
  285.  
  286.     public String[] supportedFormats()
  287.     {
  288.         String[] list = new String[4];
  289.         list[0] = EncodeFormats.TEXT;
  290.         list[1] = EncodeFormats.INI;
  291.         list[2] = EncodeFormats.DISPLAY;
  292.         list[3] = EncodeFormats.DISPLAYNAME;
  293.         return list;
  294.     }
  295.  
  296.  
  297.     // IPermission methods
  298.  
  299.     public void check (Object request) throws SecurityException
  300.     {
  301.         // Validate the request object.
  302.         if(!(request instanceof SampleRequest))
  303.             throw new IllegalArgumentException();
  304.  
  305.         // Check that the request is allowed.  If it is allowed,
  306.         // simply return.
  307.         if (items != null)
  308.         {
  309.             String strrequest = ((SampleRequest)request).item;
  310.             for (int i = 0; i < items.length; i++)
  311.             {
  312.                 if (strrequest.equals(items[i]))
  313.                     return;
  314.             }
  315.         }
  316.  
  317.         // Throwing SecurityExceptionEx will supply the caller of
  318.         // checkPermission with the class/method that failed the check.
  319.         // Anything derived from SecurityException indicates a failure.
  320.         throw new SecurityExceptionEx("Insufficient priviledge to access " + request);
  321.     }
  322.  
  323.     public IPermission copy ()
  324.     {
  325.         SamplePermission copy = new SamplePermission();
  326.  
  327.         if (this.items != null)
  328.             copy.items = (String[])items.clone();
  329.             
  330.         return copy;
  331.     }
  332.  
  333.     public IPermission combine (IPermission other)
  334.     {
  335.         if(!(other instanceof SamplePermission))
  336.             throw new IllegalArgumentException();
  337.  
  338.         SamplePermission comb = new SamplePermission();
  339.  
  340.         comb.items = PermissionUtils.combineArraysOfStrings(this.items, ((SamplePermission)other).items);
  341.         
  342.         return comb;
  343.     }
  344.  
  345.     public int compareSet (Object other)
  346.     {
  347.         if(!(other instanceof SamplePermission))
  348.             throw new IllegalArgumentException();
  349.  
  350.         SamplePermission target = (SamplePermission)other;
  351.  
  352.         int cmp = EMPTY;
  353.  
  354.         cmp = PermissionUtils.compareArraysOfStrings(this.items, target.items, cmp);
  355.  
  356.         // Comparisons of additional aspects of the permission may be
  357.         // chained together in a similar fashion.  Note that the initial
  358.         // result is EMPTY, which conceptually means that up until the first
  359.         // item of the permission, they are simply considered empty.
  360.         // For example, to compare two extra booleans:
  361.         //
  362.         // cmp = PermissionUtils.compareBooleans(this.canDoSomething, target.canDoSomething, cmp);
  363.  
  364.         // For items of types that do not have predefined comparators in
  365.         // PermissionUtils, you will need to implement your own comparator
  366.         // that produces a SetComparison result.  The result is combined
  367.         // with the results of the comparisons of the other aspects using
  368.         // PermissionUtils.mergeComparisonResults, ex.:
  369.         //
  370.         // int complexcmp = compareComplexItems(this.complexitem, target.complexitem);
  371.         // cmp = PermissionUtils.mergeComparisonResults(complexcmp, cmp);
  372.  
  373.         return cmp;
  374.     }
  375.  
  376.  
  377.     public String toString ()
  378.     {
  379.         return "SamplePermission: items="+PermissionUtils.ArraytoString(items);
  380.     }
  381. }
  382.  
  383.