/** * Creates a new FTPFile instance by converting the given file: URI into an abstract pathname. * * <p>FTP URI protocol:<br> * ftp:// [ userName [ : password ] @ ] host [ : port ][ / path ] * * <p>example:<br> * ftp://[email protected]:21/pub/testfile.txt * * @param uri An absolute, hierarchical URI using a supported scheme. * @throws NullPointerException if <code>uri</code> is <code>null</code>. * @throws IllegalArgumentException If the preconditions on the parameter do not hold. */ public FTPFile(URI uri) throws IOException, URISyntaxException { super(uri); if (uri.getScheme().equals("ftp")) { String userinfo = uri.getUserInfo(); if (userinfo != null) { int index = userinfo.indexOf(":"); if (index >= 0) { setFileSystem( new FTPFileSystem( new FTPAccount( uri.getHost(), uri.getPort(), userinfo.substring(0, index - 1), userinfo.substring(index + 1), uri.getPath()))); } else { setFileSystem( new FTPFileSystem( new FTPAccount(uri.getHost(), uri.getPort(), userinfo, "", uri.getPath()))); } } else { fileSystem = new FTPFileSystem( new FTPAccount(uri.getHost(), uri.getPort(), null, "", uri.getPath())); } setFileName(uri.getPath()); ftpClient = ((FTPFileSystem) fileSystem).getFTPClient(); } else { throw new URISyntaxException(uri.toString(), "Wrong URI scheme"); } }
private boolean identifyNewCommitResource( HttpServletRequest request, HttpServletResponse response, Repository db, String newCommit) throws ServletException { try { URI u = getURI(request); IPath p = new Path(u.getPath()); IPath np = new Path("/"); // $NON-NLS-1$ for (int i = 0; i < p.segmentCount(); i++) { String s = p.segment(i); if (i == 2) { s += ".." + newCommit; // $NON-NLS-1$ } np = np.append(s); } if (p.hasTrailingSeparator()) np = np.addTrailingSeparator(); URI nu = new URI( u.getScheme(), u.getUserInfo(), u.getHost(), u.getPort(), np.toString(), u.getQuery(), u.getFragment()); JSONObject result = new JSONObject(); result.put(ProtocolConstants.KEY_LOCATION, nu); OrionServlet.writeJSONResponse( request, response, result, JsonURIUnqualificationStrategy.ALL_NO_GIT); response.setHeader( ProtocolConstants.HEADER_LOCATION, resovleOrionURI(request, nu).toString()); return true; } catch (Exception e) { return statusHandler.handleRequest( request, response, new ServerStatus( IStatus.ERROR, HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "An error occured when identifying a new Commit resource.", e)); } }
public static String convertUrlToBaseStringURI(URL url) { URI uri = null; try { uri = url.toURI(); } catch (URISyntaxException e1) { e1.printStackTrace(); } String scheme = uri.getScheme().toLowerCase(); String host = uri.getHost().toLowerCase(); int port = uri.getPort(); if ((scheme.equals(HTTP_PROTOCOL) && port == HTTP_DEFAULT_PORT) || (scheme.equals(HTTPS_PROTOCOL) && port == HTTPS_DEFAULT_PORT)) { port = -1; } URI baseUri = null; try { baseUri = new URI(scheme, null, host, port, uri.getPath(), null, null); } catch (URISyntaxException e) { e.printStackTrace(); } return baseUri.toString(); }
/** * Decodes a Pop3Store URI. * * <p>Possible forms: * * <pre> * pop3://authType:user:password@server:port * ConnectionSecurity.NONE * pop3+tls+://authType:user:password@server:port * ConnectionSecurity.STARTTLS_REQUIRED * pop3+ssl+://authType:user:password@server:port * ConnectionSecurity.SSL_TLS_REQUIRED * </pre> * * e.g. * * <pre>pop3://PLAIN:admin:[email protected]:12345</pre> */ public static ServerSettings decodeUri(String uri) { String host; int port; ConnectionSecurity connectionSecurity; String username = null; String password = null; String clientCertificateAlias = null; URI pop3Uri; try { pop3Uri = new URI(uri); } catch (URISyntaxException use) { throw new IllegalArgumentException("Invalid Pop3Store URI", use); } String scheme = pop3Uri.getScheme(); /* * Currently available schemes are: * pop3 * pop3+tls+ * pop3+ssl+ * * The following are obsolete schemes that may be found in pre-existing * settings from earlier versions or that may be found when imported. We * continue to recognize them and re-map them appropriately: * pop3+tls * pop3+ssl */ if (scheme.equals("pop3")) { connectionSecurity = ConnectionSecurity.NONE; port = Type.POP3.defaultPort; } else if (scheme.startsWith("pop3+tls")) { connectionSecurity = ConnectionSecurity.STARTTLS_REQUIRED; port = Type.POP3.defaultPort; } else if (scheme.startsWith("pop3+ssl")) { connectionSecurity = ConnectionSecurity.SSL_TLS_REQUIRED; port = Type.POP3.defaultTlsPort; } else { throw new IllegalArgumentException("Unsupported protocol (" + scheme + ")"); } host = pop3Uri.getHost(); if (pop3Uri.getPort() != -1) { port = pop3Uri.getPort(); } AuthType authType = AuthType.PLAIN; if (pop3Uri.getUserInfo() != null) { int userIndex = 0, passwordIndex = 1; String userinfo = pop3Uri.getUserInfo(); String[] userInfoParts = userinfo.split(":"); if (userInfoParts.length > 2 || userinfo.endsWith(":")) { // If 'userinfo' ends with ":" the password is empty. This can only happen // after an account was imported (so authType and username are present). userIndex++; passwordIndex++; authType = AuthType.valueOf(userInfoParts[0]); } username = decodeUtf8(userInfoParts[userIndex]); if (userInfoParts.length > passwordIndex) { if (authType == AuthType.EXTERNAL) { clientCertificateAlias = decodeUtf8(userInfoParts[passwordIndex]); } else { password = decodeUtf8(userInfoParts[passwordIndex]); } } } return new ServerSettings( ServerSettings.Type.POP3, host, port, connectionSecurity, authType, username, password, clientCertificateAlias); }