View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4   //  ------------------------------------------------------------------------
5   //  All rights reserved. This program and the accompanying materials
6   //  are made available under the terms of the Eclipse Public License v1.0
7   //  and Apache License v2.0 which accompanies this distribution.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.quickstart;
20  
21  import java.io.File;
22  import java.net.URI;
23  import java.net.URL;
24  import java.nio.file.Path;
25  
26  import org.eclipse.jetty.util.URIUtil;
27  import org.eclipse.jetty.util.log.Log;
28  import org.eclipse.jetty.util.log.Logger;
29  import org.eclipse.jetty.util.resource.Resource;
30  
31  /**
32   * Normalize Attribute to String.
33   * <p>Replaces and expands:
34   * <ul>
35   * <li>${WAR}</li>
36   * <li>${jetty.base}</li>
37   * <li>${jetty.home}</li>
38   * <li>${user.home}</li>
39   * <li>${user.dir}</li>
40   * </ul>
41   */
42  public class AttributeNormalizer
43  {
44      private static final Logger LOG = Log.getLogger(AttributeNormalizer.class);
45      private final Path _warPath;
46      private final Path _jettyBasePath;
47      private final Path _jettyHomePath;
48      private final Path _userHomePath;
49      private final Path _userDirPath;
50      
51      
52      public AttributeNormalizer(Resource baseResource)
53      {
54          try
55          {
56              _warPath=baseResource==null?null:baseResource.getFile().toPath();
57              _jettyBasePath=systemPath("jetty.base");
58              _jettyHomePath=systemPath("jetty.home");
59              _userHomePath=systemPath("user.home");
60              _userDirPath=systemPath("user.dir");
61          }
62          catch(Exception e)
63          {
64              throw new IllegalArgumentException(e);
65          }
66      }
67      
68      private static Path systemPath(String property) throws Exception
69      {
70          String p=System.getProperty(property);
71          if (p!=null)
72              return new File(p).getAbsoluteFile().getCanonicalFile().toPath();
73          return null;
74      }
75     
76      public String normalize(Object o)
77      {
78          try
79          {
80              // Find a URI
81              URI uri=null;
82              if (o instanceof URI)
83                  uri=(URI)o;
84              else if (o instanceof URL)
85                  uri = ((URL)o).toURI();
86              else if (o instanceof File)
87                  uri = ((File)o).toURI();
88              else
89              {
90                  String s=o.toString();
91                  uri=new URI(s);
92                  if (uri.getScheme()==null)
93                      return s;
94              }
95              
96              if ("jar".equalsIgnoreCase(uri.getScheme()))
97              {
98                  String raw = uri.getRawSchemeSpecificPart();
99                  int bang=raw.indexOf("!/");
100                 String normal=normalize(raw.substring(0,bang));
101                 String suffix=raw.substring(bang);
102                 return "jar:"+normal+suffix;
103             }
104             else if ("file".equalsIgnoreCase(uri.getScheme()))
105             {
106                 return "file:"+normalizePath(new File(uri).toPath());
107             }
108             
109         }
110         catch(Exception e)
111         {
112             LOG.warn(e);
113         }
114         return String.valueOf(o);
115     }
116     
117     public String normalizePath(Path path)
118     {
119         if (_warPath!=null && path.startsWith(_warPath))
120             return URIUtil.addPaths("${WAR}",_warPath.relativize(path).toString());
121         if (_jettyBasePath!=null && path.startsWith(_jettyBasePath))
122             return URIUtil.addPaths("${jetty.base}",_jettyBasePath.relativize(path).toString());
123         if (_jettyHomePath!=null && path.startsWith(_jettyHomePath))
124             return URIUtil.addPaths("${jetty.home}",_jettyHomePath.relativize(path).toString());
125         if (_userHomePath!=null && path.startsWith(_userHomePath))
126             return URIUtil.addPaths("${user.home}",_userHomePath.relativize(path).toString());
127         if (_userDirPath!=null && path.startsWith(_userDirPath))
128             return URIUtil.addPaths("${user.dir}",_userDirPath.relativize(path).toString());
129         
130         return path.toString();
131     }
132     
133     
134     public String expand(String s)
135     {
136         int i=s.indexOf("${");
137         if (i<0)
138             return s;
139         int e=s.indexOf('}',i+3);
140         String prop=s.substring(i+2,e);
141         switch(prop)
142         {
143             case "WAR":
144                 return s.substring(0,i)+_warPath+expand(s.substring(e+1));
145             case "jetty.base":
146                 return s.substring(0,i)+_jettyBasePath+expand(s.substring(e+1));
147             case "jetty.home":
148                 return s.substring(0,i)+_jettyHomePath+expand(s.substring(e+1));
149             case "user.home":
150                 return s.substring(0,i)+_userHomePath+expand(s.substring(e+1));
151             case "user.dir":
152                 return s.substring(0,i)+_userDirPath+expand(s.substring(e+1));
153             default:
154                 return s;
155         }
156     }
157 }