private void connectFtpServer() { try { ftpClient = new FTPClient(); FTPClientConfig ftpClientConfig = new FTPClientConfig(); ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID()); ftpClient.configure(ftpClientConfig); URL url = getURL(); if (url.getPort() <= 0) { ftpClient.connect(url.getHost()); } else { ftpClient.connect(url.getHost(), url.getPort()); } if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { throw new VFSRuntimeException("连接失败!"); } if (url.getUserInfo() != null) { String userInfo[] = url.getUserInfo().split(":"); String userName = null; String password = null; if (userInfo.length >= 1) { userName = userInfo[0]; } if (userInfo.length >= 2) { password = userInfo[1]; } if (!ftpClient.login(userName, password)) { throw new VFSRuntimeException("登录失败:" + url.toString()); } if (!ftpClient.setFileType(FTP.BINARY_FILE_TYPE)) { throw new VFSRuntimeException("设置二进制类型失败"); } ftpClient.setBufferSize(BUF_SIZE); ftpClient.setControlEncoding("utf-8"); } } catch (Exception e) { throw new VFSRuntimeException(e); } }
/** * Comparison that does not consider Ref. * * @param url1 the url1 * @param url2 the url2 * @return true, if successful */ public static boolean sameNoRefURL(URL url1, URL url2) { return Objects.equals(url1.getHost(), url2.getHost()) && Objects.equals(url1.getProtocol(), url2.getProtocol()) && (url1.getPort() == url2.getPort()) && Objects.equals(url1.getFile(), url2.getFile()) && Objects.equals(url1.getUserInfo(), url2.getUserInfo()); }
public PasswordAuthentication getAuthentication(String realm, URL tracker) { if (user_name == null || password == null) { String user_info = tracker.getUserInfo(); if (user_info == null) { return (null); } String user_bit = user_info; String pw_bit = ""; int pos = user_info.indexOf(':'); if (pos != -1) { user_bit = user_info.substring(0, pos); pw_bit = user_info.substring(pos + 1); } return (new PasswordAuthentication(user_bit, pw_bit.toCharArray())); } return (new PasswordAuthentication(user_name, password.toCharArray())); }
public static String encodeDocumentUrl(String urlString) { try { URL url = new URL(urlString); URI uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toASCIIString(); } catch (MalformedURLException e) { if (Log.LOGD) Log.d(ExoUtils.class.getSimpleName(), e.getMessage(), Log.getStackTraceString(e)); return null; } catch (URISyntaxException e) { if (Log.LOGD) Log.d(ExoUtils.class.getSimpleName(), e.getMessage(), Log.getStackTraceString(e)); return null; } }
public String evaluate(String urlStr, String partToExtract) { if (urlStr == null || partToExtract == null) { return null; } if (lastUrlStr == null || !urlStr.equals(lastUrlStr)) { try { url = new URL(urlStr); } catch (Exception e) { return null; } } lastUrlStr = urlStr; if (partToExtract.equals("HOST")) return url.getHost(); if (partToExtract.equals("PATH")) return url.getPath(); if (partToExtract.equals("QUERY")) return url.getQuery(); if (partToExtract.equals("REF")) return url.getRef(); if (partToExtract.equals("PROTOCOL")) return url.getProtocol(); if (partToExtract.equals("FILE")) return url.getFile(); if (partToExtract.equals("AUTHORITY")) return url.getAuthority(); if (partToExtract.equals("USERINFO")) return url.getUserInfo(); return null; }
/** * Builds an URI from an URL (with a handle for URLs not compliant with RFC 2396). * * @param url an URL * @return an URI * @throws URISyntaxException if the URI is invalid and could not be repaired */ public static URI urlToUri(URL url) throws URISyntaxException { URI uri; try { // Possible failing step. uri = url.toURI(); } catch (Exception e) { // URL did not comply with RFC 2396 => illegal non-escaped characters. try { uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); } catch (Exception e1) { throw new URISyntaxException(String.valueOf(url), "Broken URL."); } } uri = uri.normalize(); return uri; }
public static URL getURL(double latitude, double longitude, String output) throws MalformedURLException, IOException { StringBuilder stringBuilder = new StringBuilder(URL_PREFIX); stringBuilder.append("?"); stringBuilder.append("output=" + output + "&"); stringBuilder.append("location=" + latitude + "," + longitude + "&"); stringBuilder.append("key=" + PRIVATE_KEY); URL url = new URL(stringBuilder.toString()); System.out.println(String.format("getProtocol %s", url.getProtocol())); System.out.println(String.format("getHost %s", url.getHost())); System.out.println(String.format("getPath %s", url.getPath())); System.out.println(String.format("getPort %s", url.getPort())); System.out.println(String.format("getDefaultPort %s", url.getDefaultPort())); System.out.println(String.format("getQuery %s", url.getQuery())); System.out.println(String.format("getAuthority %s", url.getAuthority())); System.out.println(String.format("getRef %s", url.getRef())); System.out.println(String.format("getUserInfo %s", url.getUserInfo())); System.out.println(String.format("getFile %s", url.getFile())); System.out.println(String.format("getContent %s", url.getContent())); System.out.println(String.format("toExternalForm %s", url.toExternalForm())); System.out.println("---------------"); return url; }
static String getUserinfo(String surl) { try { URL url = new URL(surl); return url.getUserInfo(); } catch (MalformedURLException mue) { return null; } }
private void parseAuth() { errReceiver.info( new SAXParseException(WscompileMessages.WSIMPORT_READING_AUTH_FILE(authFile), null)); BufferedReader in; try { in = new BufferedReader(new InputStreamReader(new FileInputStream(authFile), "UTF-8")); } catch (UnsupportedEncodingException e) { error(new SAXParseException(e.getMessage(), null)); return; } catch (FileNotFoundException e) { error( new SAXParseException( WscompileMessages.WSIMPORT_AUTH_FILE_NOT_FOUND(authFile, defaultAuthfile), null, e)); return; } String text; LocatorImpl locator = new LocatorImpl(); try { int lineno = 1; locator.setSystemId(authFile.getCanonicalPath()); while ((text = in.readLine()) != null) { locator.setLineNumber(lineno++); try { URL url = new URL(text); String authinfo = url.getUserInfo(); if (authinfo != null) { int i = authinfo.indexOf(':'); if (i >= 0) { String user = authinfo.substring(0, i); String password = authinfo.substring(i + 1); authInfo.add(new AuthInfo(new URL(text), user, password)); } else { error( new SAXParseException( WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator)); } } else { error( new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(url), locator)); } } catch (NumberFormatException e) { error(new SAXParseException(WscompileMessages.WSIMPORT_ILLEGAL_AUTH_INFO(text), locator)); } } in.close(); } catch (IOException e) { error( new SAXParseException( WscompileMessages.WSIMPORT_FAILED_TO_PARSE(authFile, e.getMessage()), locator)); } }
/** * Gets the no ref form. * * @param url the url * @return the no ref form */ public static String getNoRefForm(URL url) { String host = url.getHost(); int port = url.getPort(); String portText = port == -1 ? "" : ":" + port; String userInfo = url.getUserInfo(); String userInfoText = (userInfo == null) || (userInfo.length() == 0) ? "" : userInfo + "@"; String hostPort = (host == null) || (host.length() == 0) ? "" : "//" + userInfoText + host + portText; return url.getProtocol() + ":" + hostPort + url.getFile(); }
/** * @param args * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException { // TODO Auto-generated method stub URL url = new URL("http://www.baidu.com"); URL tuto = new URL(url, "tutorial.intro.html#DOWNLOADING"); System.out.println("protocal=" + tuto.getProtocol()); System.out.println("host=" + tuto.getHost()); System.out.println("port=" + tuto.getPort()); System.out.println("Authority=" + tuto.getAuthority()); System.out.println("Path=" + tuto.getPath()); System.out.println("UserInfo=" + tuto.getUserInfo()); System.out.println("DefaultPort=" + tuto.getDefaultPort()); }
private static URL _encode(URL url) throws Exception { URI uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return new URL(uri.toASCIIString()); }
public static void main(String[] args) throws IOException { URL url = new URL("http://www.javajeff.com/articles/articles/html"); System.out.println("Authority = " + url.getAuthority()); System.out.println("Default port = " + url.getDefaultPort()); System.out.println("File = " + url.getFile()); System.out.println("Host = " + url.getHost()); System.out.println("Path = " + url.getPath()); System.out.println("Port = " + url.getPort()); System.out.println("Protocol = " + url.getProtocol()); System.out.println("Query = " + url.getQuery()); System.out.println("Ref = " + url.getRef()); System.out.println("User Info = " + url.getUserInfo()); }
/* (non-Javadoc) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { try { if (m_client == null) { connect(); } int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":"); HttpGet request = new HttpGet( URIUtils.createURI( m_url.getProtocol(), m_url.getHost(), port, m_url.getPath(), m_url.getQuery(), null)); if (userInfo != null) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo[0], userInfo[1]); request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false)); } HttpResponse response = m_client.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }
/** * Encodes passed URL * * @param url Prepared URL */ protected String encodeUrl(URL url) { try { URI uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); return uri.toASCIIString(); } catch (URISyntaxException e) { return url.toString(); } }
@Test public void test_javaNetUrl() throws Exception { java.net.URL url = new java.net.URL( "http://*****:*****@10.20.130.230:20880/context/path?version=1.0.0&application=morgan#anchor1"); assertEquals("http", url.getProtocol()); assertEquals("admin:hello1234", url.getUserInfo()); assertEquals("10.20.130.230", url.getHost()); assertEquals(20880, url.getPort()); assertEquals("/context/path", url.getPath()); assertEquals("version=1.0.0&application=morgan", url.getQuery()); assertEquals("anchor1", url.getRef()); assertEquals("admin:[email protected]:20880", url.getAuthority()); assertEquals("/context/path?version=1.0.0&application=morgan", url.getFile()); }
public void validate(JTextField component) throws ConfigurationException { String value = component.getText(); if (StringUtils.isEmpty(value)) { return; } try { URL url = new URL(value); String userInfo = url.getUserInfo(); if (StringUtils.isEmpty(userInfo)) { return; } throw new ConfigurationException( "Credentials should not be embedded in the url. Use the above form instead."); } catch (MalformedURLException ex) { throw new ConfigurationException(String.format("URL '%s' is malformed", value)); } }
/** 设置现成的uri。 */ public final URIBroker setServerURI(String uriString) { URL uri; try { uri = new URL(assertNotNull(trimToNull(uriString), "serverURI")); } catch (MalformedURLException e) { throw new IllegalArgumentException(e.getMessage()); } String serverScheme = uri.getProtocol(); String[] userInfo = StringUtil.split(uri.getUserInfo(), ":"); String serverName = uri.getHost(); int serverPort = uri.getPort(); if (serverScheme != null) { setServerScheme(serverScheme); } if (!isEmptyArray(userInfo)) { if (userInfo.length > 0) { setLoginUser(userInfo[0]); } if (userInfo.length > 1) { setLoginPassword(userInfo[1]); } } if (serverName != null) { setServerName(serverName); } if (serverPort > 0) { setServerPort(serverPort); } setServerURI(uri); new URIBrokerQueryStringParser().parse(uri.getQuery()); setReference(uri.getRef()); return this; }
public IMAPConnection(URL url) throws MessagingException { Properties props = new Properties(); Session session = Session.getDefaultInstance(props, null); // session.setDebug(true); // try { // session.setDebugOut(new PrintStream(new FileOutputStream("foobar.txt"))); // } catch (FileNotFoundException e) { // e.printStackTrace(); //To change body of catch statement use File | Settings | File // Templates. // } store = (IMAPStore) session.getStore(url.getProtocol()); String username = null; String password = null; String userinfo = url.getUserInfo(); if (userinfo != null) { String[] parts = userinfo.split(":"); username = parts[0].replace('=', '@'); if (parts.length > 1) password = parts[1]; } if (username == null) username = System.getProperty("imapfs.username"); if (password == null) password = System.getProperty("imapfs.password"); store.connect(url.getHost(), username, password); String path = url.getPath(); if (path.startsWith("/")) path = path.substring(1); String[] parts = path.split(";"); if (parts != null && parts.length > 0) this.folder = store.getFolder(parts[0]); else this.folder = store.getDefaultFolder(); if (!folder.exists()) folder.create(Folder.HOLDS_MESSAGES); folder.open(Folder.READ_WRITE); }
/* * (non-Javadoc) * * @see * org.eclipse.ecf.provider.filetransfer.outgoing.AbstractOutgoingFileTransfer * #openStreams() */ protected void openStreams() throws IncomingFileTransferException { try { // Set input stream from local file final URL url = getRemoteFileURL(); this.username = url.getUserInfo(); scpUtil = new ScpUtil(this); final Session s = scpUtil.getSession(); s.connect(); final String command = SCP_COMMAND + scpUtil.trimTargetFile(url.getPath()); channel = s.openChannel(SCP_EXEC); ((ChannelExec) channel).setCommand(command); channel.connect(); final InputStream ins = channel.getInputStream(); responseStream = channel.getOutputStream(); scpUtil.sendZeroToStream(responseStream); final int c = checkAck(ins); if (c != 'C') throw new IOException(Messages.ScpRetrieveFileTransfer_EXCEPTION_SCP_PROTOCOL); // read '0644 ' final byte[] buf = new byte[1024]; ins.read(buf, 0, 5); setFileLength(readFileSize(ins, buf)); readFileName(ins, buf); // set input stream for reading rest of file remoteFileContents = ins; scpUtil.sendZeroToStream(responseStream); fireReceiveStartEvent(); } catch (final Exception e) { channel = null; username = null; throw new IncomingFileTransferException( NLS.bind( Messages.ScpRetrieveFileTransfer_EXCEPTION_CONNECTING, getRemoteFileURL().toString()), e); } }
/** * FtpURLConnection constructor comment. * * @param url */ protected FtpURLConnection(URL url) { super(url); hostName = url.getHost(); String parse = url.getUserInfo(); if (parse != null) { int split = parse.indexOf(':'); if (split >= 0) { username = parse.substring(0, split); password = parse.substring(split + 1); } else { username = parse; } } uri = null; try { uri = url.toURI(); } catch (URISyntaxException e) { // do nothing. } }
public Object fetch(String address) throws MalformedURLException, IOException { try { URL url = new URL(address); URI uri = new URI( url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef()); url = uri.toURL(); Log.i("Url:", url + ""); Object content = url.getContent(); return content; } catch (Exception e) { e.printStackTrace(); } return null; }
public static URL encodeURL(URL url, int port) throws MalformedURLException { // file String path = url.getPath(); // String file=url.getFile(); String query = url.getQuery(); String ref = url.getRef(); String user = url.getUserInfo(); if (port <= 0) port = url.getPort(); // decode path if (!StringUtil.isEmpty(path)) { int sqIndex = path.indexOf(';'); String q = null; if (sqIndex != -1) { q = path.substring(sqIndex + 1); path = path.substring(0, sqIndex); } StringBuilder res = new StringBuilder(); StringList list = ListUtil.toListTrim(path, '/'); String str; while (list.hasNext()) { str = list.next(); // str=URLDecoder.decode(str); if (StringUtil.isEmpty(str)) continue; res.append("/"); res.append(escapeQSValue(str)); } if (StringUtil.endsWith(path, '/')) res.append('/'); path = res.toString(); if (sqIndex != -1) { path += decodeQuery(q, ';'); } } // decode query query = decodeQuery(query, '?'); String file = path + query; // decode ref/anchor if (ref != null) { file += "#" + escapeQSValue(ref); } // user/pasword if (!StringUtil.isEmpty(user)) { int index = user.indexOf(':'); if (index != -1) { user = escapeQSValue(user.substring(0, index)) + ":" + escapeQSValue(user.substring(index + 1)); } else user = escapeQSValue(user); String strUrl = getProtocol(url) + "://" + user + "@" + url.getHost(); if (port > 0) strUrl += ":" + port; strUrl += file; return new URL(strUrl); } // port if (port <= 0) return new URL(url.getProtocol(), url.getHost(), file); return new URL(url.getProtocol(), url.getHost(), port, file); }
/** * Creates a new URL to the specified resource {@code spec}. This URL is relative to the given * {@code context}. The {@code handler} will be used to parse the URL string representation. If * this argument is {@code null} the default {@code URLStreamHandler} will be used. If the * protocol of the parsed URL does not match with the protocol of the context URL, then the newly * created URL is absolute and bases only on the given URL represented by {@code spec}. Otherwise * the protocol is defined by the context URL. * * @param context the URL which is used as the context. * @param spec the URL string representation which has to be parsed. * @param handler the specific stream handler to be used by this URL. * @throws IOException if the given string {@code spec} could not be parsed as a URL or an invalid * protocol has been found. */ public URL(URL context, String spec) throws MalformedURLException { if (spec == null) { throw new MalformedURLException(); } spec = spec.trim(); this.spec = spec; // The spec includes a protocol if it includes a colon character // before the first occurrence of a slash character. Note that, // "protocol" is the field which holds this URLs protocol. int index; try { index = spec.indexOf(':'); } catch (NullPointerException e) { throw new MalformedURLException(e.toString()); } int startIPv6Addr = spec.indexOf('['); if (index >= 0) { if ((startIPv6Addr == -1) || (index < startIPv6Addr)) { protocol = spec.substring(0, index); // According to RFC 2396 scheme part should match // the following expression: // alpha *( alpha | digit | "+" | "-" | "." ) char c = protocol.charAt(0); boolean valid = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); for (int i = 1; valid && (i < protocol.length()); i++) { c = protocol.charAt(i); valid = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z') || ('0' <= c && c <= '9') || (c == '+') || (c == '-') || (c == '.'); } if (!valid) { protocol = null; index = -1; } else { // Ignore case in protocol names. // Scheme is defined by ASCII characters. protocol = protocol.toLowerCase(); } } } if (protocol != null) { // If the context was specified, and it had the same protocol // as the spec, then fill in the receiver's slots from the values // in the context but still allow them to be over-ridden later // by the values in the spec. if (context != null && protocol.equals(context.getProtocol())) { String cPath = context.getPath(); if (cPath != null && cPath.startsWith("/")) { set( protocol, context.getHost(), context.getPort(), context.getAuthority(), context.getUserInfo(), cPath, context.getQuery(), null); } } } else { // If the spec did not include a protocol, then the context // *must* be specified. Fill in the receiver's slots from the // values in the context, but still allow them to be over-ridden // by the values in the ("relative") spec. if (context == null) { throw new MalformedURLException("Protocol not found: " + spec); } set( context.getProtocol(), context.getHost(), context.getPort(), context.getAuthority(), context.getUserInfo(), context.getPath(), context.getQuery(), null); } if (port < -1) { throw new MalformedURLException("Port out of range: " + port); } }
protected URLConnection openConnection(URL u) throws IOException { // This looking for accessKey id and accessKey secret code is based // on code from hadoop S3. String accessKey = null; String secretAccessKey = null; String userInfo = u.getUserInfo(); if (userInfo != null) { int index = userInfo.indexOf(':'); if (index != -1) { accessKey = userInfo.substring(0, index); secretAccessKey = userInfo.substring(index + 1); } else { accessKey = userInfo; } } if (accessKey == null) { accessKey = System.getProperty("aws.access.key.id"); } if (secretAccessKey == null) { secretAccessKey = System.getProperty("aws.access.key.secret"); } if (accessKey == null && secretAccessKey == null) { throw new IllegalArgumentException("AWS " + "Access Key ID and Secret Access Key " + "must be specified as the username " + "or password (respectively) of a s3 URL, " + "or by setting the " + "aws.access.key.id or " + "aws.access.key.secret properties (respectively)."); } else if (accessKey == null) { throw new IllegalArgumentException("AWS " + "Access Key ID must be specified " + "as the username of a s3 URL, or by setting the " + "aws.access.key.id property."); } else if (secretAccessKey == null) { throw new IllegalArgumentException("AWS " + "Secret Access Key must be specified " + "as the password of a s3 URL, or by setting the " + "aws.access.key.secret property."); } RestS3Service s3Service; try { s3Service = new RestS3Service( new AWSCredentials(accessKey, secretAccessKey)); } catch (S3ServiceException e) { e.printStackTrace(); throw new IOException(e.toString()); } InputStream is = null; try { // This opens the stream to the bucket/key object. S3Object s3obj = s3Service.getObject(new S3Bucket(u.getHost()), u.getPath().substring(1) /* Skip starting '/' character */); is = s3obj.getDataInputStream(); } catch (S3ServiceException e) { e.printStackTrace(); throw new IOException(e.toString()); } final InputStream inputStream = is; return new URLConnection(u) { private InputStream is = inputStream; @Override public InputStream getInputStream() throws IOException { return this.is; } @Override public void connect() throws IOException { // Nothing to do. When we give back this object, we're // connected. } }; }
private Path download(PluginHandle pluginHandle, Terminal terminal) throws IOException { Path pluginFile = pluginHandle.newDistroFile(environment); HttpDownloadHelper downloadHelper = new HttpDownloadHelper(); boolean downloaded = false; boolean verified = false; HttpDownloadHelper.DownloadProgress progress; if (outputMode == OutputMode.SILENT) { progress = new HttpDownloadHelper.NullProgress(); } else { progress = new HttpDownloadHelper.VerboseProgress(terminal.writer()); } // first, try directly from the URL provided if (url != null) { URL pluginUrl = url; boolean isSecureProcotol = "https".equalsIgnoreCase(pluginUrl.getProtocol()); boolean isAuthInfoSet = !Strings.isNullOrEmpty(pluginUrl.getUserInfo()); if (isAuthInfoSet && !isSecureProcotol) { throw new IOException("Basic auth is only supported for HTTPS!"); } terminal.println("Trying %s ...", pluginUrl.toExternalForm()); try { downloadHelper.download(pluginUrl, pluginFile, progress, this.timeout); downloaded = true; terminal.println("Verifying %s checksums if available ...", pluginUrl.toExternalForm()); Tuple<URL, Path> sha1Info = pluginHandle.newChecksumUrlAndFile(environment, pluginUrl, "sha1"); verified = downloadHelper.downloadAndVerifyChecksum( sha1Info.v1(), pluginFile, sha1Info.v2(), progress, this.timeout, HttpDownloadHelper.SHA1_CHECKSUM); Tuple<URL, Path> md5Info = pluginHandle.newChecksumUrlAndFile(environment, pluginUrl, "md5"); verified = verified || downloadHelper.downloadAndVerifyChecksum( md5Info.v1(), pluginFile, md5Info.v2(), progress, this.timeout, HttpDownloadHelper.MD5_CHECKSUM); } catch (ElasticsearchTimeoutException | ElasticsearchCorruptionException e) { throw e; } catch (Exception e) { // ignore terminal.println("Failed: %s", ExceptionsHelper.detailedMessage(e)); } } else { if (PluginHandle.isOfficialPlugin( pluginHandle.name, pluginHandle.user, pluginHandle.version)) { checkForOfficialPlugins(pluginHandle.name); } } if (!downloaded && url == null) { // We try all possible locations for (URL url : pluginHandle.urls()) { terminal.println("Trying %s ...", url.toExternalForm()); try { downloadHelper.download(url, pluginFile, progress, this.timeout); downloaded = true; terminal.println("Verifying %s checksums if available ...", url.toExternalForm()); Tuple<URL, Path> sha1Info = pluginHandle.newChecksumUrlAndFile(environment, url, "sha1"); verified = downloadHelper.downloadAndVerifyChecksum( sha1Info.v1(), pluginFile, sha1Info.v2(), progress, this.timeout, HttpDownloadHelper.SHA1_CHECKSUM); Tuple<URL, Path> md5Info = pluginHandle.newChecksumUrlAndFile(environment, url, "md5"); verified = verified || downloadHelper.downloadAndVerifyChecksum( md5Info.v1(), pluginFile, md5Info.v2(), progress, this.timeout, HttpDownloadHelper.MD5_CHECKSUM); break; } catch (ElasticsearchTimeoutException | ElasticsearchCorruptionException e) { throw e; } catch (Exception e) { terminal.println(VERBOSE, "Failed: %s", ExceptionsHelper.detailedMessage(e)); } } } if (!downloaded) { // try to cleanup what we downloaded IOUtils.deleteFilesIgnoringExceptions(pluginFile); throw new IOException( "failed to download out of all possible locations..., use --verbose to get detailed information"); } if (verified == false) { terminal.println( "NOTE: Unable to verify checksum for downloaded plugin (unable to find .sha1 or .md5 file to verify)"); } return pluginFile; }
protected HttpDownloadResult doDownloadUrl( final URL url, final Integer timeout, final String username, final String password, final HttpHeader httpHeader) throws IOException { logger.trace("downloadUrl started"); final Duration duration = durationUtil.getDuration(); InputStream inputStream = null; ByteArrayOutputStream outputStream = null; try { final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); if (username != null && password != null || url.getUserInfo() != null) { final String stringUserIdPassword = url.getUserInfo() != null ? url.getUserInfo() : username + ":" + password; final String base64UserIdPassword = base64Util.encode(stringUserIdPassword.getBytes("ASCII")); connection.setRequestProperty("Authorization", "Basic " + base64UserIdPassword); } // connection.setConnectTimeout(timeout); // connection.setReadTimeout(timeout); connection.setRequestProperty("User-Agent", USERAGENT); if (httpHeader != null) { for (final String httpHeaderField : httpHeader.getKeys()) { connection.setRequestProperty(httpHeaderField, httpHeader.getValue(httpHeaderField)); } } connection.connect(); final int responseCode; try { responseCode = connection.getResponseCode(); } catch (RuntimeException e) { final HttpDownloadResult httpDownloadResult = new HttpDownloadResultImpl(url, duration.getTime(), null, null, null, null, -1); logger.trace("downloadUrl " + url + " failed"); return httpDownloadResult; } try { final String contentType = connection.getContentType(); final Encoding contentEncoding = extractEncoding(connection, contentType); final Map<String, List<String>> headers = connection.getHeaderFields(); try { inputStream = connection.getInputStream(); outputStream = new ByteArrayOutputStream(); streamUtil.copy(inputStream, outputStream); final HttpDownloadResult httpDownloadResult = new HttpDownloadResultImpl( url, duration.getTime(), outputStream.toByteArray(), contentType, contentEncoding, headers, responseCode); logger.trace("downloadUrl " + url + " finished"); return httpDownloadResult; } catch (FileNotFoundException e) { final HttpDownloadResult httpDownloadResult = new HttpDownloadResultImpl( url, duration.getTime(), null, contentType, contentEncoding, headers, responseCode); logger.trace("downloadUrl " + url + " failed"); return httpDownloadResult; } } catch (IOException e) { final HttpDownloadResult httpDownloadResult = new HttpDownloadResultImpl( url, duration.getTime(), null, null, null, null, responseCode); logger.trace("downloadUrl " + url + " failed"); return httpDownloadResult; } } finally { if (inputStream != null) inputStream.close(); if (outputStream != null) outputStream.close(); } }
/* (non-Javadoc) * @see java.net.URLConnection#getInputStream() */ @Override public InputStream getInputStream() throws IOException { try { if (m_client == null) { connect(); } // Build URL int port = m_url.getPort() > 0 ? m_url.getPort() : m_url.getDefaultPort(); URIBuilder ub = new URIBuilder(); ub.setPort(port); ub.setScheme(m_url.getProtocol()); ub.setHost(m_url.getHost()); ub.setPath(m_url.getPath()); ub.setQuery(m_url.getQuery()); // Build Request HttpRequestBase request = null; if (m_request != null && m_request.getMethod().equalsIgnoreCase("post")) { final Content cnt = m_request.getContent(); HttpPost post = new HttpPost(ub.build()); ContentType contentType = ContentType.create(cnt.getType()); LOG.info("Processing POST request for %s", contentType); if (contentType .getMimeType() .equals(ContentType.APPLICATION_FORM_URLENCODED.getMimeType())) { FormFields fields = JaxbUtils.unmarshal(FormFields.class, cnt.getData()); post.setEntity(fields.getEntity()); } else { StringEntity entity = new StringEntity(cnt.getData(), contentType); post.setEntity(entity); } request = post; } else { request = new HttpGet(ub.build()); } if (m_request != null) { // Add Custom Headers for (Header header : m_request.getHeaders()) { request.addHeader(header.getName(), header.getValue()); } } // Add User Authentication String[] userInfo = m_url.getUserInfo() == null ? null : m_url.getUserInfo().split(":"); if (userInfo != null) { UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(userInfo[0], userInfo[1]); request.addHeader(BasicScheme.authenticate(credentials, "UTF-8", false)); } // Get Response HttpResponse response = m_client.execute(request); return response.getEntity().getContent(); } catch (Exception e) { throw new IOException( "Can't retrieve " + m_url.getPath() + " from " + m_url.getHost() + " because " + e.getMessage(), e); } }
@Override public Proxy createProxy() { String proxy = System.getenv("HTTP_PROXY"); if (proxy == null || proxy.isEmpty()) { proxy = System.getProperty("HTTP_PROXY"); } try { URL proxyUrl = new URL(proxy); // Case for Proxy authentication required try { StringTokenizer tokenizedUrl = new StringTokenizer(proxyUrl.getUserInfo().toString(), ":"); if (tokenizedUrl.hasMoreTokens()) { final String authUser = tokenizedUrl.nextToken(); if (tokenizedUrl.hasMoreTokens()) { final String authPassword = tokenizedUrl.nextToken(); if ((proxy != null && !proxy.isEmpty()) && (authUser != null && !authUser.isEmpty()) && (authUser != null && !authPassword.isEmpty())) { Authenticator.setDefault( new Authenticator() { public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(authUser, authPassword.toCharArray()); } }); System.setProperty("http.proxyUser", authUser); System.setProperty("http.proxyPassword", authPassword); } } else { LOGGER.warn( "Proxy Authentication did not contain a valid password parameter\nSkipping Authenticated proxy step."); } } else { LOGGER.info( "Proxy did not contain authentication parameters - assuming non-authenticated proxy"); } } catch (IllegalArgumentException e) { LOGGER.warn( "Malformed Proxy Authentication Credentials for HTTP Proxy in " + this.getClass().getName(), e); } // Configuring proxy if (proxy != null && !proxy.isEmpty()) { return new Proxy( Proxy.Type.HTTP, new InetSocketAddress(proxyUrl.getHost(), proxyUrl.getPort())); } } catch (MalformedURLException ex) { LOGGER.error("Malformed HTTP Proxy for " + this.getClass().getName(), ex); } catch (NullPointerException npe) { LOGGER.error( "Unexpectedly, something in your proxy configuration was blank or misreferenced for " + this.getClass().getName(), npe); } return Proxy.NO_PROXY; }
@Override public void execute() throws MojoExecutionException, MojoFailureException { if (isIgnoreProject()) return; try { boolean newUserAdded = false; fabricServer = mavenSettings.getServer(serverId); if (Strings.isNullOrBlank(consoleUrl)) { consoleUrl = DEFAULT_CONSOLE_URL; } // we may have username and password from consoleUrl String jolokiaUsername = null; String jolokiaPassword = null; try { URL url = new URL(consoleUrl); String s = url.getUserInfo(); if (Strings.isNotBlank(s) && s.indexOf(':') > 0) { int idx = s.indexOf(':'); jolokiaUsername = s.substring(0, idx); jolokiaPassword = s.substring(idx + 1); } } catch (MalformedURLException e) { throw new IllegalArgumentException("Option consoleUrl is invalid due " + e.getMessage()); } // jolokia url overrides username/password configured in maven settings if (jolokiaUsername != null) { if (fabricServer == null) { fabricServer = new Server(); } getLog() .info( "Using username: "******" and password from provided consoleUrl option"); fabricServer.setUsername(jolokiaUsername); fabricServer.setPassword(jolokiaPassword); } if (fabricServer == null) { boolean create = false; if (mavenSettings.isInteractiveMode() && mavenSettingsWriter != null) { System.out.println("Maven settings file: " + mavenSettingsFile.getAbsolutePath()); System.out.println(); System.out.println(); System.out.println( "There is no <server> section in your ~/.m2/settings.xml file for the server id: " + serverId); System.out.println(); System.out.println( "You can enter the username/password now and have the settings.xml updated or you can do this by hand if you prefer."); System.out.println(); while (true) { String value = readInput("Would you like to update the settings.xml file now? (y/n): ") .toLowerCase(); if (value.startsWith("n")) { System.out.println(); System.out.println(); break; } else if (value.startsWith("y")) { create = true; break; } } if (create) { System.out.println("Please let us know the login details for this server: " + serverId); System.out.println(); String userName = readInput("Username: "******"Password: "******"Repeat Password: "******"Passwords do not match, please try again."); password = readPassword("Password: "******"Repeat Password: "******".backup-" + counter++ + ".xml"); if (!backupFile.exists()) { System.out.println( "Copied original: " + mavenSettingsFile.getAbsolutePath() + " to: " + backupFile.getAbsolutePath()); Files.copy(mavenSettingsFile, backupFile); break; } } } Map<String, Object> config = new HashMap<String, Object>(); mavenSettingsWriter.write(mavenSettingsFile, config, mavenSettings); System.out.println("Updated settings file: " + mavenSettingsFile.getAbsolutePath()); System.out.println(); newUserAdded = true; } } } if (fabricServer == null) { String message = "No <server> element can be found in ~/.m2/settings.xml for the server <id>" + serverId + "</id> so we cannot connect to fabric8!\n\n" + "Please add the following to your ~/.m2/settings.xml file (using the correct user/password values):\n\n" + "<servers>\n" + " <server>\n" + " <id>" + serverId + "</id>\n" + " <username>admin</username>\n" + " <password>admin</password>\n" + " </server>\n" + "</servers>\n"; getLog().error(message); throw new MojoExecutionException(message); } if (!isIgnoreProject()) { uploadAppZip(newUserAdded); } else { getLog().info("Ignoring this project so not uploading the App Zip"); } } catch (MojoExecutionException e) { throw e; } catch (Exception e) { throw new MojoExecutionException("Error executing", e); } }