1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.utils.security;
18
19 import java.net.URI;
20
21 import org.gwe.utils.IOUtils;
22 import org.gwe.utils.rex.REXException;
23 import org.gwe.utils.rex.REXParser;
24 import org.gwe.utils.rex.config.REXConfig4Class;
25 import org.gwe.utils.rex.config.REXConfig4Field;
26 import org.gwe.utils.rex.config.REXConfig4String;
27
28
29
30
31
32 @REXConfig4Class(rexPieces={"scheme", ThinURI.SCHEME_SEPARATOR, "host", IOUtils.FILE_SEPARATOR, "path"})
33 public class ThinURI {
34
35 public static final String SCHEME_SEPARATOR = "://";
36
37 public static ThinURI createBlind(String uri) {
38 try {
39 return create(uri);
40 } catch (REXException e) {
41 return null;
42 }
43 }
44
45 public static ThinURI create(String uri) throws REXException {
46 if (!uri.contains(SCHEME_SEPARATOR)) uri += ProtocolScheme.FILE.toURIStr(uri);
47 return REXParser.createModel(ThinURI.class, uri);
48 }
49
50 public static String asNormalizedFileURI(String hostName, String file) {
51
52 if (!file.contains(SCHEME_SEPARATOR))
53 file = ProtocolScheme.FILE.toURIStr(file);
54
55
56 if (hostName != null && !hostName.equals("") && !hostName.equals("localhost") && !hostName.equals("127.0.0.1"))
57 file = file.replace(ProtocolScheme.FILE.toURIStr(), ProtocolScheme.SFTP.toURIStr(hostName));
58
59 return file;
60 }
61
62 @REXConfig4String(pattern=@REXConfig4Field(field="[^:]*"))
63 private String scheme;
64
65 @REXConfig4String(pattern=@REXConfig4Field(field="[^/]*"))
66 private String host;
67
68 @REXConfig4String(pattern=@REXConfig4Field(field=".*"))
69 private String path;
70
71 private String toString = null;
72
73 public ThinURI() {}
74
75 public ThinURI(ProtocolScheme scheme, String location, String path) {
76 this(scheme.toString(), location, path);
77 }
78
79 public ThinURI(String scheme, String location, String path) {
80 this.scheme = scheme;
81 this.host = location;
82 setPath(path);
83 }
84
85 public String getScheme() {
86 return scheme;
87 }
88
89 public ProtocolScheme getProtocolScheme() {
90 return ProtocolScheme.valueOf(scheme.toUpperCase());
91 }
92
93 public void setScheme(String scheme) {
94 this.scheme = scheme;
95 }
96
97 public String getHost() {
98 return host;
99 }
100
101 public void setHost(String location) {
102 this.host = location;
103 }
104
105 public String getPath() {
106 return path;
107 }
108
109 public void setPath(String path) {
110 if (path == null) path = "";
111 this.path = IOUtils.concatenatePaths("", path);
112 }
113
114 public String toString() {
115 if (toString == null)
116 toString = scheme.toString() + SCHEME_SEPARATOR + IOUtils.concatenatePaths(host, path.toString());
117 return toString;
118 }
119
120 public URI toURI() {
121 return URI.create(toString());
122 }
123
124 public ThinURI clone() {
125 return new ThinURI(scheme, host, path);
126 }
127
128 @Override
129 public int hashCode() {
130 final int prime = 31;
131 int result = 1;
132 result = prime * result + ((host == null) ? 0 : host.hashCode());
133 result = prime * result + ((path == null) ? 0 : path.hashCode());
134 result = prime * result + ((scheme == null) ? 0 : scheme.hashCode());
135 return result;
136 }
137
138 @Override
139 public boolean equals(Object obj) {
140 if (this == obj)
141 return true;
142 if (obj == null)
143 return false;
144 if (getClass() != obj.getClass())
145 return false;
146 ThinURI other = (ThinURI) obj;
147 if (host == null) {
148 if (other.host != null)
149 return false;
150 } else if (!host.equals(other.host))
151 return false;
152 if (path == null) {
153 if (other.path != null)
154 return false;
155 } else if (!path.equals(other.path))
156 return false;
157 if (scheme == null) {
158 if (other.scheme != null)
159 return false;
160 } else if (!scheme.equals(other.scheme))
161 return false;
162 return true;
163 }
164
165 public static void main(String[] args) throws Exception {
166 String uriStr = ProtocolScheme.SFTP.toURIStr("birn-cluster0.nbirn.net", "/~/path/to/file");
167 ThinURI uri1 = ThinURI.create(uriStr);
168 ProtocolScheme protScheme = uri1.getProtocolScheme();
169 System.out.println(uri1);
170 System.out.println(ThinURI.create("ssh://localhost/"));
171 }
172 }
173