List of usage examples for java.net URL getHost
public String getHost()
From source file:biz.neustar.nexus.plugins.gitlab.GitlabAuthenticatingRealmIT.java
License:asdf
@Test public void testPlugin() throws Exception { assertTrue(nexus().isRunning());/*from w w w . jav a 2 s.c o m*/ URL nexusUrl = nexus().getUrl(); URI uri = new URIBuilder().setHost(nexusUrl.getHost()).setPath(nexusUrl.getPath()) .setPort(nexusUrl.getPort()).setParameters(URLEncodedUtils.parse(nexusUrl.getQuery(), UTF_8)) .setScheme(nexusUrl.getProtocol()).setUserInfo("jdamick", "asdfasdfasdf").build() .resolve("content/groups/public/"); HttpClient httpclient = HttpClientBuilder.create().build(); {// request 1 HttpGet req1 = new HttpGet(uri); HttpResponse resp1 = httpclient.execute(req1); assertEquals(200, resp1.getStatusLine().getStatusCode()); RecordedRequest request = server.takeRequest(); // 1 request recorded assertEquals("/api/v3/session", request.getPath()); req1.releaseConnection(); } // failure checks { // request 2 HttpGet req2 = new HttpGet(uri); HttpResponse resp2 = httpclient.execute(req2); assertEquals(401, resp2.getStatusLine().getStatusCode()); req2.releaseConnection(); } { // request 3 HttpGet req3 = new HttpGet(uri); HttpResponse resp3 = httpclient.execute(req3); assertEquals(401, resp3.getStatusLine().getStatusCode()); req3.releaseConnection(); } }
From source file:org.apache.oltu.oauth2.client.demo.controller.TokenController.java
@RequestMapping public ModelAndView authorize(@ModelAttribute("oauthParams") OAuthParams oauthParams, HttpServletRequest req) throws OAuthSystemException, IOException { try {//from w w w .j a va 2 s .c o m Utils.validateTokenParams(oauthParams); OAuthClientRequest request = OAuthClientRequest.tokenLocation(oauthParams.getTokenEndpoint()) .setClientId(oauthParams.getClientId()).setClientSecret(oauthParams.getClientSecret()) .setRedirectURI(oauthParams.getRedirectUri()).setCode(oauthParams.getAuthzCode()) .setGrantType(GrantType.AUTHORIZATION_CODE).buildBodyMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); String app = Utils.findCookieValue(req, "app"); OAuthAccessTokenResponse oauthResponse = null; Class<? extends OAuthAccessTokenResponse> cl = OAuthJSONAccessTokenResponse.class; if (Utils.FACEBOOK.equalsIgnoreCase(app)) { cl = GitHubTokenResponse.class; } else if (Utils.GITHUB.equalsIgnoreCase(app)) { cl = GitHubTokenResponse.class; } else if (Utils.GOOGLE.equalsIgnoreCase(app)) { cl = OpenIdConnectResponse.class; } oauthResponse = client.accessToken(request, cl); oauthParams.setAccessToken(oauthResponse.getAccessToken()); oauthParams.setExpiresIn(oauthResponse.getExpiresIn()); oauthParams.setRefreshToken(Utils.isIssued(oauthResponse.getRefreshToken())); if (Utils.GOOGLE.equalsIgnoreCase(app)) { OpenIdConnectResponse openIdConnectResponse = ((OpenIdConnectResponse) oauthResponse); JWT idToken = openIdConnectResponse.getIdToken(); oauthParams.setIdToken(idToken.getRawString()); oauthParams.setHeader(new JWTHeaderWriter().write(idToken.getHeader())); oauthParams.setClaimsSet(new JWTClaimsSetWriter().write(idToken.getClaimsSet())); URL url = new URL(oauthParams.getTokenEndpoint()); oauthParams .setIdTokenValid(openIdConnectResponse.checkId(url.getHost(), oauthParams.getClientId())); } return new ModelAndView("get_resource"); } catch (ApplicationException e) { oauthParams.setErrorMessage(e.getMessage()); return new ModelAndView("request_token"); } catch (OAuthProblemException e) { StringBuffer sb = new StringBuffer(); sb.append("</br>"); sb.append("Error code: ").append(e.getError()).append("</br>"); sb.append("Error description: ").append(e.getDescription()).append("</br>"); sb.append("Error uri: ").append(e.getUri()).append("</br>"); sb.append("State: ").append(e.getState()).append("</br>"); oauthParams.setErrorMessage(sb.toString()); return new ModelAndView("get_authz"); } }
From source file:com.digitalpebble.storm.crawler.bolt.URLPartitionerBolt.java
@Override public void execute(Tuple tuple) { String url = tuple.getStringByField("url"); Metadata metadata = null;//from w w w. j av a 2s . c o m if (tuple.contains("metadata")) metadata = (Metadata) tuple.getValueByField("metadata"); // maybe there is a field metadata but it can be null // or there was no field at all if (metadata == null) metadata = Metadata.empty; String partitionKey = null; String host = ""; // IP in metadata? if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP)) { String ip_provided = metadata.getFirstValue("ip"); if (StringUtils.isNotBlank(ip_provided)) { partitionKey = ip_provided; eventCounter.scope("provided").incrBy(1); } } if (partitionKey == null) { URL u; try { u = new URL(url); host = u.getHost(); } catch (MalformedURLException e1) { eventCounter.scope("Invalid URL").incrBy(1); LOG.warn("Invalid URL: {}", url); // ack it so that it doesn't get replayed _collector.ack(tuple); return; } } // partition by hostname if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_HOST)) partitionKey = host; // partition by domain : needs fixing else if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_DOMAIN)) { partitionKey = PaidLevelDomain.getPLD(host); } // partition by IP if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP) && partitionKey == null) { // try to get it from cache first partitionKey = cache.get(host); if (partitionKey != null) { eventCounter.scope("from cache").incrBy(1); } else { try { long start = System.currentTimeMillis(); final InetAddress addr = InetAddress.getByName(host); partitionKey = addr.getHostAddress(); long end = System.currentTimeMillis(); LOG.debug("Resolved IP {} in {} msec for : {}", partitionKey, (end - start), url); // add to cache cache.put(host, partitionKey); } catch (final Exception e) { eventCounter.scope("Unable to resolve IP").incrBy(1); LOG.warn("Unable to resolve IP for: {}", host); _collector.ack(tuple); return; } } } LOG.debug("Partition Key for: {} > {}", url, partitionKey); _collector.emit(tuple, new Values(url, partitionKey, metadata)); _collector.ack(tuple); }
From source file:com.digitalpebble.stormcrawler.bolt.URLPartitionerBolt.java
@Override public void execute(Tuple tuple) { String url = tuple.getStringByField("url"); Metadata metadata = null;//from w ww. j av a 2s .c o m if (tuple.contains("metadata")) metadata = (Metadata) tuple.getValueByField("metadata"); // maybe there is a field metadata but it can be null // or there was no field at all if (metadata == null) metadata = Metadata.empty; String partitionKey = null; String host = ""; // IP in metadata? if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP)) { String ip_provided = metadata.getFirstValue("ip"); if (StringUtils.isNotBlank(ip_provided)) { partitionKey = ip_provided; eventCounter.scope("provided").incrBy(1); } } if (partitionKey == null) { URL u; try { u = new URL(url); host = u.getHost(); } catch (MalformedURLException e1) { eventCounter.scope("Invalid URL").incrBy(1); LOG.warn("Invalid URL: {}", url); // ack it so that it doesn't get replayed _collector.ack(tuple); return; } } // partition by hostname if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_HOST)) partitionKey = host; // partition by domain : needs fixing else if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_DOMAIN)) { partitionKey = PaidLevelDomain.getPLD(host); } // partition by IP if (mode.equalsIgnoreCase(Constants.PARTITION_MODE_IP) && partitionKey == null) { // try to get it from cache first partitionKey = cache.get(host); if (partitionKey != null) { eventCounter.scope("from cache").incrBy(1); } else { try { long start = System.currentTimeMillis(); final InetAddress addr = InetAddress.getByName(host); partitionKey = addr.getHostAddress(); long end = System.currentTimeMillis(); LOG.debug("Resolved IP {} in {} msec for : {}", partitionKey, end - start, url); // add to cache cache.put(host, partitionKey); } catch (final Exception e) { eventCounter.scope("Unable to resolve IP").incrBy(1); LOG.warn("Unable to resolve IP for: {}", host); _collector.ack(tuple); return; } } } LOG.debug("Partition Key for: {} > {}", url, partitionKey); _collector.emit(tuple, new Values(url, partitionKey, metadata)); _collector.ack(tuple); }
From source file:com.test.controller.TokenController.java
@RequestMapping("/get_token") public ModelAndView authorize(HttpServletRequest req) throws OAuthSystemException, IOException { ModelMap map = new ModelMap(); try {/*from ww w. ja v a2 s. c om*/ logger.info("==============================get_token============================================="); String code = req.getParameter("code"); logger.info("code=" + code); OAuthClientRequest request = OAuthClientRequest.tokenLocation(tokenEndpoint).setClientId(clientId) .setClientSecret(clientSecret).setRedirectURI(redirectUri).setScope(scope).setCode(code) .setGrantType(GrantType.AUTHORIZATION_CODE).buildBodyMessage(); OAuthClient client = new OAuthClient(new URLConnectionClient()); OAuthAccessTokenResponse oauthResponse = null; Class<? extends OAuthAccessTokenResponse> cl = OpenIdConnectResponse.class; oauthResponse = client.accessToken(request, cl); logger.info("getAccessToken" + oauthResponse.getAccessToken()); logger.info("getExpiresIn" + oauthResponse.getExpiresIn()); logger.info("getRefreshToken" + Oauth2Utils.isIssued(oauthResponse.getRefreshToken())); req.getSession().setAttribute(OAuth.OAUTH_ACCESS_TOKEN, oauthResponse.getAccessToken()); OpenIdConnectResponse openIdConnectResponse = ((OpenIdConnectResponse) oauthResponse); JWT idToken = openIdConnectResponse.getIdToken(); logger.info("idToken" + idToken.getRawString()); logger.info("getHeader " + idToken.getHeader()); logger.info("getHeader " + new JWTHeaderWriter().write(idToken.getHeader())); logger.info("getClaimsSet " + idToken.getClaimsSet()); logger.info("getClaimsSet " + new JWTClaimsSetWriter().write(idToken.getClaimsSet())); URL url = new URL(tokenEndpoint); logger.info("getIdTokenValid " + openIdConnectResponse.checkId(url.getHost(), clientId)); return new ModelAndView(new RedirectView("get_resource")); } catch (OAuthProblemException e) { logger.error(e.getMessage()); StringBuffer sb = new StringBuffer(); sb.append("</br>"); sb.append("Error code: ").append(e.getError()).append("</br>"); sb.append("Error description: ").append(e.getDescription()).append("</br>"); sb.append("Error uri: ").append(e.getUri()).append("</br>"); sb.append("State: ").append(e.getState()).append("</br>"); map.put("msg", sb.toString()); return new ModelAndView("index", map); } }
From source file:org.ebayopensource.twin.TwinConnection.java
public TwinConnection(URL url) { if (url.getPath().endsWith("/")) try {/*from w w w . j a v a2 s . c o m*/ url = new URL(url.getProtocol(), url.getHost(), url.getPort(), url.getPath().substring(0, url.getPath().length() - 1)); } catch (MalformedURLException e) { throw new RuntimeException(e); } this.url = url; }
From source file:edu.ucsb.eucalyptus.admin.server.extensions.store.ImageStoreServiceImpl.java
private NameValuePair[] getFinalParameters(Method method, String uri, Parameter[] params, UserInfoWeb user) throws MalformedURLException { if (params == null) { // Simplify the logic below. params = new Parameter[0]; }/*from ww w.j av a 2 s . c o m*/ URL url = new URL(uri); SignatureGenerator signatureGenerator = new SignatureGenerator(method.toString(), url.getHost(), url.getPort(), url.getPath()); Parameter[] finalParams = new Parameter[params.length + 7]; System.arraycopy(params, 0, finalParams, 0, params.length); int i = params.length; finalParams[i++] = new Parameter("SignatureMethod", SIGNATURE_METHOD); finalParams[i++] = new Parameter("SignatureVersion", SIGNATURE_VERSION); finalParams[i++] = new Parameter("Version", API_VERSION); finalParams[i++] = new Parameter("ClientId", user.getQueryId()); finalParams[i++] = new Parameter("Expires", new Long((System.currentTimeMillis() / 1000) + EXPIRES_DELAY_SECONDS).toString()); finalParams[i++] = new Parameter("Nonce", new Long(System.nanoTime()).toString()); for (Parameter param : finalParams) { if (param != null) { signatureGenerator.addParameter(param.getName(), param.getValue()); } } String signature = signatureGenerator.getSignature(user.getSecretKey()); finalParams[i++] = new Parameter("Signature", signature); return getNameValuePairs(finalParams); }
From source file:com.toolsverse.io.FtpUtils.java
/** * Connects to the FTP server.// ww w .jav a2 s.c o m * * @param urlStr the url * @param loginId the login id * @param loginPswd the login ppassword * @param passiveMode the passive mode flag * @throws Exception in case of any error */ public void connect(String urlStr, String loginId, String loginPswd, boolean passiveMode) throws Exception { boolean success = false; URL url = new URL(urlStr); String host = url.getHost(); int port = url.getPort(); if (port == -1) port = FTP_PORT; ftpClient.connect(host, port); int reply = ftpClient.getReplyCode(); if (FTPReply.isPositiveCompletion(reply)) { success = ftpClient.login(!Utils.isNothing(loginId) ? loginId : "anonymous", Utils.makeString(loginPswd)); if (!success) disconnect(); else { ftpClient.setFileType(FTP.BINARY_FILE_TYPE); if (passiveMode) ftpClient.enterLocalPassiveMode(); } } }
From source file:org.dataconservancy.access.connector.HttpDcsSearchIteratorTest.java
@Override protected DcsConnectorConfig getConnectorConfig() { DcsConnectorConfig config = new DcsConnectorConfig(); try {//from ww w . j a v a 2 s . c om URL u = new URL( String.format(accessServiceUrl, testServer.getServiceHostName(), testServer.getServicePort())); config.setScheme(u.getProtocol()); config.setHost(u.getHost()); config.setPort(u.getPort()); config.setContextPath(u.getPath()); config.setMaxOpenConn(1); } catch (MalformedURLException e) { fail("Unable to construct connector configuration: " + e.getMessage()); } return config; }
From source file:com.iflytek.spider.net.BasicURLNormalizer.java
public String normalize(String urlString) throws MalformedURLException { if ("".equals(urlString)) // permit empty return urlString; urlString = urlString.trim(); // remove extra spaces URL url = new URL(urlString); String protocol = url.getProtocol(); String host = url.getHost(); int port = url.getPort(); String file = url.getFile();// w w w . ja v a 2 s. c o m boolean changed = false; if (!urlString.startsWith(protocol)) // protocol was lowercased changed = true; if ("http".equals(protocol) || "ftp".equals(protocol)) { if (host != null) { String newHost = host.toLowerCase(); // lowercase host if (!host.equals(newHost)) { host = newHost; changed = true; } } if (port == url.getDefaultPort()) { // uses default port port = -1; // so don't specify it changed = true; } if (file == null || "".equals(file)) { // add a slash file = "/"; changed = true; } if (url.getRef() != null) { // remove the ref changed = true; } // check for unnecessary use of "/../" String file2 = substituteUnnecessaryRelativePaths(file); if (!file.equals(file2)) { changed = true; file = file2; } } if (changed) urlString = new URL(protocol, host, port, file).toString(); return urlString; }