View Javadoc
1   /**
2    * Copyright 2014 Internet2
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   * $Id: Morph.java,v 1.3 2008-10-29 05:32:23 mchyzer Exp $
18   * 
19   * Copyright University of Pennsylvania 2004
20   */
21  package edu.internet2.middleware.grouperInstaller.morphString;
22  
23  import java.io.File;
24  import java.io.IOException;
25  import java.util.Properties;
26  
27  import edu.internet2.middleware.grouperInstaller.util.GrouperInstallerUtils;
28  
29  /**
30   * @author Chris Hyzer
31   */
32  public class Morph {
33  
34    /**
35     * 
36     */
37    public static final String ENCRYPT_KEY = "encrypt.key";
38  
39    /**
40     * strip the prefix off
41     * @param string
42     * @param prefix
43     * @return the string without the prefix
44     */
45    public static String stripPrefix(String string, String prefix) {
46      if (string == null || prefix == null) {
47        return string;
48      }
49      if (string.startsWith(prefix)) {
50        return string.substring(prefix.length(), string.length());
51      }
52      return string;
53    }
54    
55    
56    /**
57     * 
58     * @param file
59     *          is the file to read into a string
60     * 
61     * @return String
62     */
63    public static String readFileIntoStringUtf8(File file) {
64    
65      if (file == null) {
66        return null;
67      }
68      try {
69        return GrouperInstallerUtils.readFileToString(file, "UTF-8");
70      } catch (IOException ioe) {
71        throw new RuntimeException(ioe);
72      }
73    }
74    
75    
76    public static String readFromFileIfFileUtf8(String in, boolean disableExternalFileLookup) {
77      
78      if (in == null || "".equals(in)) {
79        return in;
80      }
81      
82      boolean isFile = false;
83      if (in.startsWith("file:")) {
84        isFile = true;
85        in = stripPrefix(in, "file:");
86        File file = new File(in);
87        if (!file.exists() || !file.isFile()) {
88          throw new RuntimeException("Cant find or read file: '" + in + "'");
89        }
90      } else {
91        if (!disableExternalFileLookup) {
92          File file = new File(in);
93          if (file.exists() && file.isFile()) {
94            isFile = true;
95          }
96        }
97      }
98      
99      //see if it is a file reference
100     if (isFile) {
101       //read the contents of the file into a string
102       return readFileIntoStringUtf8(new File(in));
103     }
104     return in;
105   
106   }
107 
108   /** if testing, and not relying on morph key being there, use this */
109   public static String testMorphKey = null;
110 
111   /**
112    * Constructor for Morph.
113    */
114   private Morph() {
115   }
116 
117 }