List of usage examples for org.springframework.util StringUtils cleanPath
public static String cleanPath(String path)
From source file:org.springframework.cloud.config.server.resource.GenericResourceRepository.java
/** * Identifies invalid resource paths. By default rejects: * <ul>//from w ww. j a v a 2 s . c om * <li>Paths that contain "WEB-INF" or "META-INF" * <li>Paths that contain "../" after a call to * {@link org.springframework.util.StringUtils#cleanPath}. * <li>Paths that represent a {@link org.springframework.util.ResourceUtils#isUrl * valid URL} or would represent one after the leading slash is removed. * </ul> * <p> * <strong>Note:</strong> this method assumes that leading, duplicate '/' or control * characters (e.g. white space) have been trimmed so that the path starts predictably * with a single '/' or does not have one. * @param path the path to validate * @return {@code true} if the path is invalid, {@code false} otherwise * @since 3.0.6 */ protected boolean isInvalidPath(String path) { if (path.contains("WEB-INF") || path.contains("META-INF")) { if (logger.isWarnEnabled()) { logger.warn("Path with \"WEB-INF\" or \"META-INF\": [" + path + "]"); } return true; } if (path.contains(":/")) { String relativePath = (path.charAt(0) == '/' ? path.substring(1) : path); if (ResourceUtils.isUrl(relativePath) || relativePath.startsWith("url:")) { if (logger.isWarnEnabled()) { logger.warn("Path represents URL or has \"url:\" prefix: [" + path + "]"); } return true; } } if (path.contains("..") && StringUtils.cleanPath(path).contains("../")) { if (logger.isWarnEnabled()) { logger.warn("Path contains \"../\" after call to StringUtils#cleanPath: [" + path + "]"); } return true; } return false; }
From source file:org.springframework.cloud.config.server.support.AbstractScmAccessor.java
protected File getWorkingDirectory() { if (this.uri.startsWith("file:")) { try {/*ww w .j a v a 2s. c om*/ return new UrlResource(StringUtils.cleanPath(this.uri)).getFile(); } catch (Exception e) { throw new IllegalStateException("Cannot convert uri to file: " + this.uri); } } return this.basedir; }
From source file:org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter.java
@Override public Object run() { RequestContext ctx = RequestContext.getCurrentContext(); final String requestURI = this.urlPathHelper.getPathWithinApplication(ctx.getRequest()); Route route = this.routeLocator.getMatchingRoute(requestURI); if (route != null) { String location = route.getLocation(); if (location != null) { ctx.put(REQUEST_URI_KEY, route.getPath()); ctx.put(PROXY_KEY, route.getId()); if (!route.isCustomSensitiveHeaders()) { this.proxyRequestHelper .addIgnoredHeaders(this.properties.getSensitiveHeaders().toArray(new String[0])); } else { this.proxyRequestHelper.addIgnoredHeaders(route.getSensitiveHeaders().toArray(new String[0])); }/*from ww w .j a v a 2 s. com*/ if (route.getRetryable() != null) { ctx.put(RETRYABLE_KEY, route.getRetryable()); } if (location.startsWith(HTTP_SCHEME + ":") || location.startsWith(HTTPS_SCHEME + ":")) { ctx.setRouteHost(getUrl(location)); ctx.addOriginResponseHeader(SERVICE_HEADER, location); } else if (location.startsWith(FORWARD_LOCATION_PREFIX)) { ctx.set(FORWARD_TO_KEY, StringUtils .cleanPath(location.substring(FORWARD_LOCATION_PREFIX.length()) + route.getPath())); ctx.setRouteHost(null); return null; } else { // set serviceId for use in filters.route.RibbonRequest ctx.set(SERVICE_ID_KEY, location); ctx.setRouteHost(null); ctx.addOriginResponseHeader(SERVICE_ID_HEADER, location); } if (this.properties.isAddProxyHeaders()) { addProxyHeaders(ctx, route); String xforwardedfor = ctx.getRequest().getHeader(X_FORWARDED_FOR_HEADER); String remoteAddr = ctx.getRequest().getRemoteAddr(); if (xforwardedfor == null) { xforwardedfor = remoteAddr; } else if (!xforwardedfor.contains(remoteAddr)) { // Prevent duplicates xforwardedfor += ", " + remoteAddr; } ctx.addZuulRequestHeader(X_FORWARDED_FOR_HEADER, xforwardedfor); } if (this.properties.isAddHostHeader()) { ctx.addZuulRequestHeader(HttpHeaders.HOST, toHostHeader(ctx.getRequest())); } } } else { log.warn("No route found for uri: " + requestURI); String fallBackUri = requestURI; String fallbackPrefix = this.dispatcherServletPath; // default fallback // servlet is // DispatcherServlet if (RequestUtils.isZuulServletRequest()) { // remove the Zuul servletPath from the requestUri log.debug("zuulServletPath=" + this.properties.getServletPath()); fallBackUri = fallBackUri.replaceFirst(this.properties.getServletPath(), ""); log.debug("Replaced Zuul servlet path:" + fallBackUri); } else { // remove the DispatcherServlet servletPath from the requestUri log.debug("dispatcherServletPath=" + this.dispatcherServletPath); fallBackUri = fallBackUri.replaceFirst(this.dispatcherServletPath, ""); log.debug("Replaced DispatcherServlet servlet path:" + fallBackUri); } if (!fallBackUri.startsWith("/")) { fallBackUri = "/" + fallBackUri; } String forwardURI = fallbackPrefix + fallBackUri; forwardURI = forwardURI.replaceAll("//", "/"); ctx.set(FORWARD_TO_KEY, forwardURI); } return null; }
From source file:org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter.java
private HttpResponse forward(HttpClient httpclient, String verb, String uri, HttpServletRequest request, MultiValueMap<String, String> headers, MultiValueMap<String, String> params, InputStream requestEntity) throws Exception { Map<String, Object> info = this.helper.debug(verb, uri, headers, params, requestEntity); URL host = RequestContext.getCurrentContext().getRouteHost(); HttpHost httpHost = getHttpHost(host); uri = StringUtils.cleanPath((host.getPath() + uri).replaceAll("/{2,}", "/")); int contentLength = request.getContentLength(); InputStreamEntity entity = new InputStreamEntity(requestEntity, contentLength, request.getContentType() != null ? ContentType.create(request.getContentType()) : null); HttpRequest httpRequest = buildHttpRequest(verb, uri, entity, headers, params); try {// w w w . jav a2 s . c o m log.debug(httpHost.getHostName() + " " + httpHost.getPort() + " " + httpHost.getSchemeName()); HttpResponse zuulResponse = forwardRequest(httpclient, httpHost, httpRequest); this.helper.appendDebug(info, zuulResponse.getStatusLine().getStatusCode(), revertHeaders(zuulResponse.getAllHeaders())); return zuulResponse; } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources // httpclient.getConnectionManager().shutdown(); } }
From source file:org.springframework.core.io.ClassPathResource.java
/** * Create a new ClassPathResource for ClassLoader usage. * A leading slash will be removed, as the ClassLoader * resource access methods will not accept it. * * @param path the absolute path within the classpath * @param classLoader the class loader to load the resource with, * or {@code null} for the thread context class loader * @see java.lang.ClassLoader#getResourceAsStream(String) */// w w w. j av a 2 s .co m public ClassPathResource(String path, ClassLoader classLoader) { Assert.notNull(path, "Path must not be null"); String pathToUse = StringUtils.cleanPath(path); if (pathToUse.startsWith("/")) { pathToUse = pathToUse.substring(1); } this.path = pathToUse; this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader()); }
From source file:org.springframework.core.io.ClassPathResource.java
/** * Create a new ClassPathResource for Class usage. * The path can be relative to the given class, * or absolute within the classpath via a leading slash. * * @param path relative or absolute path within the class path * @param clazz the class to load resources with * @see java.lang.Class#getResourceAsStream *///from w w w . ja va 2 s. c om public ClassPathResource(String path, Class clazz) { Assert.notNull(path, "Path must not be null"); this.path = StringUtils.cleanPath(path); this.clazz = clazz; }
From source file:org.springframework.core.io.ClassPathResource.java
/** * Create a new ClassPathResource with optional ClassLoader and Class. * Only for internal usage.//w w w . j a v a2s. c o m * * @param path relative or absolute path within the classpath * @param classLoader the class loader to load the resource with, if any * @param clazz the class to load resources with, if any */ protected ClassPathResource(String path, ClassLoader classLoader, Class clazz) { this.path = StringUtils.cleanPath(path); this.classLoader = classLoader; this.clazz = clazz; }
From source file:org.springframework.integration.smb.session.SmbSession.java
/** * Factory method for new SmbFile objects under this session's share for the specified path. * @param path remote path/*from w w w. ja va 2s .c om*/ * @param isDirectory Boolean object to indicate the path is a directory, may be null * @return SmbFile object for path * @throws IOException in case of I/O errors */ private SmbFile createSmbFileObject(String path, Boolean isDirectory) throws IOException { final String cleanedPath = StringUtils.cleanPath(path); if (!StringUtils.hasText(cleanedPath)) { return smbShare; } SmbFile smbFile = new SmbFile(smbShare, cleanedPath); boolean appendFileSeparator = !cleanedPath.endsWith(SMB_FILE_SEPARATOR); if (appendFileSeparator) { try { appendFileSeparator = smbFile.isDirectory() || (isDirectory != null && isDirectory); } catch (SmbException ex) { appendFileSeparator = false; } } if (appendFileSeparator) { smbFile = createSmbFileObject(cleanedPath + SMB_FILE_SEPARATOR); } if (logger.isDebugEnabled()) { logger.debug("Created new " + SmbFile.class.getName() + "[" + smbFile + "] for path [" + path + "]."); } return smbFile; }
From source file:org.springframework.integration.smb.session.SmbShare.java
public SmbShare(String url) throws IOException { super(StringUtils.cleanPath(url)); }
From source file:org.springframework.osgi.web.deployer.support.DefaultContextPathStrategy.java
private String getBundleLocation(String location) { // remove prefix (if there's any) int index = location.lastIndexOf(PREFIX_DELIMITER); String path = ((index > 0) ? location.substring(index + 1) : location); // clean up the path path = StringUtils.cleanPath(path); // check if it's a folder if (path.endsWith(SLASH)) { // remove trailing slash path = path.substring(0, path.length() - 1); int separatorIndex = path.lastIndexOf(SLASH); // if there is no other slash, consider the whole location, otherwise detect the folder path = (separatorIndex > -1 ? path.substring(separatorIndex + 1) : path); }//from w w w .j a v a 2 s .c om path = StringUtils.getFilename(path); // remove file extension path = StringUtils.stripFilenameExtension(path); if (log.isTraceEnabled()) log.trace("Bundle location [" + location + "] resulted in context path [" + path + "]"); return path; }