List of usage examples for org.springframework.web.util UriComponents getPort
public abstract int getPort();
From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java
/** * Normalize the resource string as per OIDC Discovery. * @param identifier/* w w w.ja v a 2 s . c o m*/ * @return the normalized string, or null if the string can't be normalized */ public static UriComponents normalizeResource(String identifier) { // try to parse the URI // NOTE: we can't use the Java built-in URI class because it doesn't split the parts appropriately if (Strings.isNullOrEmpty(identifier)) { logger.warn("Can't normalize null or empty URI: " + identifier); return null; // nothing we can do } else { //UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(identifier); UriComponentsBuilder builder = UriComponentsBuilder.newInstance(); Matcher m = pattern.matcher(identifier); if (m.matches()) { builder.scheme(m.group(2)); builder.userInfo(m.group(6)); builder.host(m.group(8)); String port = m.group(10); if (!Strings.isNullOrEmpty(port)) { builder.port(Integer.parseInt(port)); } builder.path(m.group(11)); builder.query(m.group(13)); builder.fragment(m.group(15)); // we throw away the hash, but this is the group it would be if we kept it } else { // doesn't match the pattern, throw it out logger.warn("Parser couldn't match input: " + identifier); return null; } UriComponents n = builder.build(); if (Strings.isNullOrEmpty(n.getScheme())) { if (!Strings.isNullOrEmpty(n.getUserInfo()) && Strings.isNullOrEmpty(n.getPath()) && Strings.isNullOrEmpty(n.getQuery()) && n.getPort() < 0) { // scheme empty, userinfo is not empty, path/query/port are empty // set to "acct" (rule 2) builder.scheme("acct"); } else { // scheme is empty, but rule 2 doesn't apply // set scheme to "https" (rule 3) builder.scheme("https"); } } // fragment must be stripped (rule 4) builder.fragment(null); return builder.build(); } }
From source file:org.appverse.web.framework.backend.frontfacade.mvc.swagger.provider.EurekaSwaggerResourcesProvider.java
private static String obtainUrlLocation(ServiceInstance instance, UriComponents current, String path, String swaggerHost) {/*www .jav a 2s . co m*/ String managementPath = ""; if (instance.getMetadata().containsKey("managementPath")) { managementPath = instance.getMetadata().get("managementPath"); } String hostUrl; if (swaggerHost != null && swaggerHost.length() > 0) { hostUrl = swaggerHost; } else { //tries to findout the host if (("https".equals(current.getScheme()) && 443 == current.getPort()) || ("http".equals(current.getScheme()) && 80 == current.getPort()) || -1 == current.getPort()) { //default ports hostUrl = String.format("%s://%s", current.getScheme(), current.getHost()); } else { //custom ports hostUrl = String.format("%s://%s:%d", current.getScheme(), current.getHost(), current.getPort()); } } return hostUrl + managementPath + path; }
From source file:org.mitre.discovery.util.WebfingerURLNormalizer.java
public static String serializeURL(UriComponents uri) { if (uri.getScheme() != null && (uri.getScheme().equals("acct") || uri.getScheme().equals("mailto") || uri.getScheme().equals("tel") || uri.getScheme().equals("device"))) { // serializer copied from HierarchicalUriComponents but with "//" removed StringBuilder uriBuilder = new StringBuilder(); if (uri.getScheme() != null) { uriBuilder.append(uri.getScheme()); uriBuilder.append(':'); }//from w ww . j av a 2s . c om if (uri.getUserInfo() != null || uri.getHost() != null) { if (uri.getUserInfo() != null) { uriBuilder.append(uri.getUserInfo()); uriBuilder.append('@'); } if (uri.getHost() != null) { uriBuilder.append(uri.getHost()); } if (uri.getPort() != -1) { uriBuilder.append(':'); uriBuilder.append(uri.getPort()); } } String path = uri.getPath(); if (StringUtils.hasLength(path)) { if (uriBuilder.length() != 0 && path.charAt(0) != '/') { uriBuilder.append('/'); } uriBuilder.append(path); } String query = uri.getQuery(); if (query != null) { uriBuilder.append('?'); uriBuilder.append(query); } if (uri.getFragment() != null) { uriBuilder.append('#'); uriBuilder.append(uri.getFragment()); } return uriBuilder.toString(); } else { return uri.toUriString(); } }
From source file:org.cloudfoundry.identity.uaa.login.test.IntegrationTestRule.java
private boolean connectionAvailable() { UriComponents components = UriComponentsBuilder.fromHttpUrl(baseUrl).build(); String host = components.getHost(); int port = components.getPort(); logger.info("Testing connectivity for " + baseUrl); try (Socket socket = new Socket(host, port)) { logger.info("Connectivity test succeeded for " + baseUrl); return true; } catch (IOException e) { logger.warn("Connectivity test failed for " + baseUrl, e); return false; }/*from www .j av a 2s.c o m*/ }
From source file:springfox.documentation.swagger2.web.Swagger2Controller.java
private String hostName(UriComponents uriComponents) { if ("DEFAULT".equals(hostNameOverride)) { String host = uriComponents.getHost(); int port = uriComponents.getPort(); if (port > -1) { return String.format("%s:%d", host, port); }/*from w w w.java 2 s .co m*/ return host; } return hostNameOverride; }
From source file:de.blizzy.documentr.markdown.macro.impl.FlattrMacroTest.java
@Test public void getHtml() { String html = macro.getHtml(macroContext); @SuppressWarnings("nls") String re = "^<a href=\"([^\"]+)\">" + "<img src=\"https://api\\.flattr\\.com/button/flattr-badge-large\\.png\"/>" + "</a>$"; //$NON-NLS-2$ assertRE(re, html);//from w w w . j av a2s . c o m Matcher matcher = Pattern.compile(re, Pattern.DOTALL).matcher(html); matcher.find(); String url = StringEscapeUtils.unescapeHtml4(matcher.group(1)); UriComponents components = UriComponentsBuilder.fromHttpUrl(url).build(); assertEquals("https", components.getScheme()); //$NON-NLS-1$ assertEquals("flattr.com", components.getHost()); //$NON-NLS-1$ assertEquals(-1, components.getPort()); assertEquals("/submit/auto", components.getPath()); //$NON-NLS-1$ MultiValueMap<String, String> params = components.getQueryParams(); assertEquals(FLATTR_USER_ID, params.getFirst("user_id")); //$NON-NLS-1$ assertEquals(PAGE_URL, params.getFirst("url")); //$NON-NLS-1$ assertEquals(PAGE_TITLE, params.getFirst("title")); //$NON-NLS-1$ assertEquals("text", params.getFirst("category")); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue(params.getFirst("tags").equals(TAG_1 + "," + TAG_2) || //$NON-NLS-1$ //$NON-NLS-2$ params.getFirst("tags").equals(TAG_2 + "," + TAG_1)); //$NON-NLS-1$ //$NON-NLS-2$ }
From source file:org.cateproject.test.functional.mockmvc.HtmlUnitRequestBuilder.java
private void ports(UriComponents uriComponents, MockHttpServletRequest result) { int serverPort = uriComponents.getPort(); result.setServerPort(serverPort);/*from w w w . jav a2s.c om*/ if (serverPort == -1) { int portConnection = webRequest.getUrl().getDefaultPort(); result.setLocalPort(serverPort); result.setRemotePort(portConnection); } else { result.setRemotePort(serverPort); } }
From source file:org.cloudfoundry.identity.client.integration.IsUAAListeningRule.java
private boolean connectionAvailable() { UriComponents components = UriComponentsBuilder.fromHttpUrl(baseUrl).build(); String host = components.getHost(); int port = components.getPort(); if (port == -1) { if (baseUrl.startsWith("https")) { port = 443;//from w w w. j ava 2 s .c om } else { port = 80; } } logger.info("Testing connectivity for " + baseUrl); try (Socket socket = new Socket(host, port)) { logger.info("Connectivity test succeeded for " + baseUrl); return true; } catch (IOException e) { logger.warn("Connectivity test failed for " + baseUrl, e); return false; } }
From source file:org.springframework.web.util.WebUtils.java
private static int getPort(UriComponents component) { int port = component.getPort(); if (port == -1) { if ("http".equals(component.getScheme())) { port = 80;//from ww w . j a va 2s . co m } else if ("https".equals(component.getScheme())) { port = 443; } } return port; }