001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008: *
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: /*
020: * FTPAddressURL.java
021: *
022: * Created on October 10, 2006, 1:29 PM
023: *
024: * To change this template, choose Tools | Template Manager
025: * and open the template in the editor.
026: */
027:
028: package org.netbeans.modules.wsdlextensions.ftp.validator;
029:
030: import org.netbeans.modules.wsdlextensions.ftp.FTPComponent;
031: import java.util.Collection;
032: import java.util.List;
033: import java.util.ResourceBundle;
034: import java.util.Vector;
035: import org.netbeans.modules.xml.xam.spi.Validator;
036:
037: /**
038: *
039: * @author jfu
040: */
041: public class FTPAddressURL implements AddressURL {
042: private String scheme;
043: private String user;
044: private String password;
045: private String host;
046: private String port;
047:
048: private String url;
049:
050: public FTPAddressURL(String url) {
051: this .url = url;
052: }
053:
054: public String getScheme() {
055: return scheme;
056: }
057:
058: public void setScheme(String scheme) {
059: this .scheme = scheme;
060: }
061:
062: public String getUser() {
063: return user;
064: }
065:
066: public void setUser(String user) {
067: this .user = user;
068: }
069:
070: public String getPassword() {
071: return password;
072: }
073:
074: public void setPassword(String password) {
075: this .password = password;
076: }
077:
078: public String getHost() {
079: return host;
080: }
081:
082: public void setHost(String host) {
083: this .host = host;
084: }
085:
086: public String getPort() {
087: return port;
088: }
089:
090: public void setPort(String port) {
091: this .port = port;
092: }
093:
094: public boolean parse(Collection<Validator.ResultItem> results,
095: Validator validator, FTPComponent target) {
096: // if missing
097: if (url == null || url.trim().length() == 0) {
098: results.add(new Validator.ResultItem(validator,
099: Validator.ResultType.ERROR, target, Util
100: .getMessage("FTPAddress.MISSING_FTP_URL")));
101: return false;
102: }
103:
104: // do not parse it if contains env vars
105: if (Util.hasMigrationEnvVar(url))
106: return true;
107:
108: // if still the place holder
109: if (url.startsWith(FTP_URL_PLACEHOLDER)) {
110: results
111: .add(new Validator.ResultItem(
112: validator,
113: Validator.ResultType.ERROR,
114: target,
115: Util
116: .getMessage("FTPAddress.REPLACE_FTP_URL_PLACEHOLDER_WITH_REAL_URL")));
117: return false;
118: }
119:
120: if (!url.startsWith(FTP_URL_PREFIX)) {
121: results.add(new Validator.ResultItem(validator,
122: Validator.ResultType.ERROR, target,
123: Util.getMessage(
124: "FTPAddress.INVALID_FTP_URL_PREFIX",
125: new Object[] { url })));
126: return false;
127: }
128: scheme = "ftp";
129: if (url.length() > FTP_URL_PREFIX.length()) {
130: String rest = url.substring(FTP_URL_PREFIX.length());
131: if (rest.indexOf(URL_PATH_DELIM) >= 0) {
132: results
133: .add(new Validator.ResultItem(
134: validator,
135: Validator.ResultType.ERROR,
136: target,
137: Util
138: .getMessage(
139: "FTPAddress.INVALID_FTP_URL_PATH_NOT_ALLOWED",
140: new Object[] { url })));
141: return false;
142: }
143:
144: int l = rest.trim().length();
145: int i = 0;
146: StringBuffer cur = new StringBuffer();
147: int at = 0;
148: int col = 0;
149: List comps = new Vector();
150: while (i < l) {
151: char c = rest.charAt(i);
152: switch (c) {
153: case '\\':
154: if (i + 1 < l) {
155: cur.append(rest.charAt(i + 1));
156: i = i + 2;
157: } else {
158: cur.append(c);
159: i++;
160: }
161: break;
162: case ':':
163: col++;
164: if (col > 1 || cur.length() == 0 /* :password and :port are invalid */) {
165: // in each part: either user:password
166: // or host:port, there can be at most 1
167: // ':' delimiter;
168: results.add(new Validator.ResultItem(validator,
169: Validator.ResultType.ERROR, target,
170: Util.getMessage(
171: "FTPAddress.MALFORMED_FTP_URL",
172: new Object[] { url })));
173: return false;
174: }
175: comps.add(cur.toString());
176: cur = new StringBuffer();
177: i++;
178: break;
179: case '@':
180: at++;
181: if (at > 1) {
182: // at most 1 '@' as delimiter;
183: results.add(new Validator.ResultItem(validator,
184: Validator.ResultType.ERROR, target,
185: Util.getMessage(
186: "FTPAddress.MALFORMED_FTP_URL",
187: new Object[] { url })));
188: return false;
189: }
190: // previously collected belongs to user_password
191: comps.add(cur.toString());
192: cur = new StringBuffer();
193: col = 0;
194: switch (comps.size()) {
195: case 1:
196: this .user = (String) comps.get(0);
197: break;
198: case 2:
199: this .user = (String) comps.get(0);
200: this .password = (String) comps.get(1);
201: break;
202: default:
203: results.add(new Validator.ResultItem(validator,
204: Validator.ResultType.ERROR, target,
205: Util.getMessage(
206: "FTPAddress.MALFORMED_FTP_URL",
207: new Object[] { url })));
208: return false;
209: }
210: comps = new Vector();
211: i++;
212: break;
213: default:
214: cur.append(c);
215: i++;
216: }
217: }
218:
219: if (cur != null && cur.length() > 0)
220: comps.add(cur.toString());
221:
222: switch (comps.size()) {
223: case 1:
224: this .host = (String) comps.get(0);
225: break;
226: case 2:
227: this .host = (String) comps.get(0);
228: this .port = (String) comps.get(1);
229: boolean goodPort = true;
230: if (port != null && port.trim().length() > 0) {
231: // must be a positive int
232: try {
233: int pt = Integer.parseInt(port);
234: if (pt <= 0)
235: goodPort = false;
236: } catch (Exception e) {
237: goodPort = false;
238: }
239: }
240:
241: if (!goodPort) {
242: results.add(new Validator.ResultItem(validator,
243: Validator.ResultType.ERROR, target,
244: Util.getMessage(
245: "FTPAddress.INVALID_PORT_IN_URL",
246: new Object[] { url })));
247: return false;
248: }
249:
250: break;
251: default:
252: results.add(new Validator.ResultItem(validator,
253: Validator.ResultType.ERROR, target, Util
254: .getMessage(
255: "FTPAddress.MALFORMED_FTP_URL",
256: new Object[] { url })));
257: return false;
258: }
259:
260: if (host == null || host.trim().length() == 0) {
261: results
262: .add(new Validator.ResultItem(
263: validator,
264: Validator.ResultType.ERROR,
265: target,
266: Util
267: .getMessage(
268: "FTPAddress.MALFORMED_FTP_URL_HOST_REQUIRED",
269: new Object[] { url })));
270: return false;
271: }
272: } else {
273: results.add(new Validator.ResultItem(validator,
274: Validator.ResultType.ERROR, target, Util
275: .getMessage("FTPAddress.MALFORMED_FTP_URL",
276: new Object[] { url })));
277: return false;
278: }
279: return true;
280: }
281:
282: }
|