Java Code Examples for org.springframework.web.util.UriUtils#encode()

The following examples show how to use org.springframework.web.util.UriUtils#encode() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PathResourceResolver.java    From spring-analysis-note with MIT License 6 votes vote down vote up
private String encodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) {
	if (shouldEncodeRelativePath(location) && request != null) {
		Charset charset = this.locationCharsets.getOrDefault(location, StandardCharsets.UTF_8);
		StringBuilder sb = new StringBuilder();
		StringTokenizer tokenizer = new StringTokenizer(path, "/");
		while (tokenizer.hasMoreTokens()) {
			String value = UriUtils.encode(tokenizer.nextToken(), charset);
			sb.append(value);
			sb.append("/");
		}
		if (!path.endsWith("/")) {
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}
	else {
		return path;
	}
}
 
Example 2
Source File: PathResourceResolver.java    From java-technology-stack with MIT License 6 votes vote down vote up
private String encodeIfNecessary(String path, @Nullable HttpServletRequest request, Resource location) {
	if (shouldEncodeRelativePath(location) && request != null) {
		Charset charset = this.locationCharsets.getOrDefault(location, StandardCharsets.UTF_8);
		StringBuilder sb = new StringBuilder();
		StringTokenizer tokenizer = new StringTokenizer(path, "/");
		while (tokenizer.hasMoreTokens()) {
			String value = UriUtils.encode(tokenizer.nextToken(), charset);
			sb.append(value);
			sb.append("/");
		}
		if (!path.endsWith("/")) {
			sb.setLength(sb.length() - 1);
		}
		return sb.toString();
	}
	else {
		return path;
	}
}
 
Example 3
Source File: SearchUtils.java    From benten with MIT License 5 votes vote down vote up
public static String generateGoogleSearchUrl(String searchString) {
    try {
        String uriQuery = UriUtils.encode(searchString, StandardCharsets.UTF_8.name());
        return SearchItems.GOOGLE_SEARCH_URL + uriQuery;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return "";
    }
}
 
Example 4
Source File: WxRedirectUtils.java    From FastBootWeixin with Apache License 2.0 5 votes vote down vote up
public static String redirect(String baseUrl, String url, String state, boolean isBase) {
    boolean isRedirect = true;
    if (url == null) {
        url = "";
    }
    if (url.startsWith(WX_OAUTH2_URL)) {
        return url;
    } else if (url.startsWith(BASE_REDIRECT)) {
        isBase = true;
        url = url.substring(3);
    } else if (url.startsWith(AUTH_REDIRECT)) {
        isBase = false;
        url = url.substring(3);
    } else if (url.startsWith(NO_REDIRECT)) {
        isRedirect = false;
        url = url.substring(3);
    }

    String redirectUrl = WxUrlUtils.absoluteUrl(baseUrl, url);
    if (!isRedirect || !WxUrlUtils.isCallbackUrl(baseUrl, redirectUrl)) {
        return redirectUrl;
    }
    try {
        redirectUrl = UriUtils.encode(redirectUrl, StandardCharsets.UTF_8.name());
    } catch (Exception e) {
        // java.io.UnsupportedEncodingException在SB2.0中不再抛出,故兼容下,处理为Exception
        // ignore it
    }
    return baseBuilder.cloneBuilder().queryParam("appid", Wx.Environment.instance().getWxAppId())
            .queryParam("redirect_uri", redirectUrl)
            .queryParam("response_type", "code")
            .queryParam("scope", isBase ? "snsapi_base" : "snsapi_userinfo")
            .queryParam("state", state).build().toUriString() + "#wechat_redirect";
}
 
Example 5
Source File: StudioLoginUrlAuthenticationEntryPoint.java    From studio with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected String determineUrlToUseForThisRequest(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) {

    String redirectParamValue = request.getContextPath() + UrlUtils.buildRequestUrl(request);
    try {
        redirectParamValue = UriUtils.encode(redirectParamValue, StandardCharsets.UTF_8.toString());
    } catch (UnsupportedEncodingException e) {
        logger.debug("Unsupported encoding for redirect query param value. Sending param without encoding it");
    }
    String redirect = super.determineUrlToUseForThisRequest(request, response, exception);
    return UriComponentsBuilder.fromPath(redirect).queryParam(PARAM_REDIRECT, redirectParamValue).toUriString();
}
 
Example 6
Source File: RedirectView.java    From spring-analysis-note with MIT License 4 votes vote down vote up
private String encodeUriVariable(String text) {
	// Strict encoding of all reserved URI characters
	return UriUtils.encode(text, StandardCharsets.UTF_8);
}
 
Example 7
Source File: RedirectView.java    From java-technology-stack with MIT License 4 votes vote down vote up
private String encodeUriVariable(String text) {
	// Strict encoding of all reserved URI characters
	return UriUtils.encode(text, StandardCharsets.UTF_8);
}
 
Example 8
Source File: TwitterOAuth.java    From webflux-twitter-demo with MIT License 4 votes vote down vote up
public String urlEncode(String source) {
    return UriUtils.encode(source, StandardCharsets.UTF_8);
}
 
Example 9
Source File: WikiFurService.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public String getUrl(String article) {
    return WIKI_URL + UriUtils.encode(article, "UTF-8");
}
 
Example 10
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
public String upload(@ValidateStringParam(name = "site_id") final String site,
                     @ValidateStringParam(name = "profile") final String profileId,
                     @ValidateStringParam(name = "path") final String path,
                     @ValidateStringParam(name = "filename") final String filename,
                     final InputStream content)
    throws WebDavException {
    WebDavProfile profile = getProfile(site, profileId);
    String uploadUrl = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
    try {
        Sardine sardine = SardineFactory.begin(profile.getUsername(), profile.getPassword());

        if(StringUtils.isNotEmpty(path)) {
            String[] folders = StringUtils.split(path, "/");

            for(String folder : folders) {
                uploadUrl += StringUtils.appendIfMissing(folder, "/");

                logger.debug("Checking folder {0}", uploadUrl);
                if(!sardine.exists(uploadUrl)) {
                    logger.debug("Creating folder {0}", uploadUrl);
                    sardine.createDirectory(uploadUrl);
                    logger.debug("Folder {0} created", uploadUrl);
                } else {
                    logger.debug("Folder {0} already exists", uploadUrl);
                }
            }
        }

        uploadUrl =  StringUtils.appendIfMissing(uploadUrl, "/");
        String fileUrl = uploadUrl + UriUtils.encode(filename, charset.name());

        logger.debug("Starting upload of file {0}", filename);
        logger.debug("Uploading file to {0}", fileUrl);

        sardine.put(fileUrl, content);
        logger.debug("Upload complete for file {0}", fileUrl);
        if(StringUtils.isNotEmpty(profile.getDeliveryBaseUrl())) {
            fileUrl = StringUtils.replaceFirst(fileUrl, profile.getBaseUrl(), profile.getDeliveryBaseUrl());
        }
        return fileUrl;
    } catch (Exception e ) {
        throw new WebDavException("Error uploading file", e);
    }
}
 
Example 11
Source File: WebDavServiceImpl.java    From studio with GNU General Public License v3.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@ValidateParams
@HasPermission(type = DefaultPermission.class, action = "webdav_write")
public WebDavItem upload(@ValidateStringParam(name = "siteId") @ProtectedResourceId("siteId") final String siteId,
                         @ValidateStringParam(name = "profileId") final String profileId,
                         @ValidateStringParam(name = "path") final String path,
                         @ValidateStringParam(name = "filename") final String filename,
                         final InputStream content)
    throws WebDavException {
    WebDavProfile profile = getProfile(siteId, profileId);
    String uploadUrl = StringUtils.appendIfMissing(profile.getBaseUrl(), "/");
    try {
        Sardine sardine = createClient(profile);

        if(StringUtils.isNotEmpty(path)) {
            String[] folders = StringUtils.split(path, "/");

            for(String folder : folders) {
                uploadUrl += StringUtils.appendIfMissing(folder, "/");

                logger.debug("Checking folder {0}", uploadUrl);
                if(!sardine.exists(uploadUrl)) {
                    logger.debug("Creating folder {0}", uploadUrl);
                    sardine.createDirectory(uploadUrl);
                    logger.debug("Folder {0} created", uploadUrl);
                } else {
                    logger.debug("Folder {0} already exists", uploadUrl);
                }
            }
        }

        uploadUrl =  StringUtils.appendIfMissing(uploadUrl, "/");
        String fileUrl = uploadUrl + UriUtils.encode(filename, charset.name());

        logger.debug("Starting upload of file {0}", filename);
        logger.debug("Uploading file to {0}", fileUrl);

        sardine.put(fileUrl, content);
        logger.debug("Upload complete for file {0}", fileUrl);

        return new WebDavItem(filename, String.format(urlPattern, profileId, path, filename), false);
    } catch (Exception e ) {
        throw new WebDavException("Error uploading file", e);
    }
}
 
Example 12
Source File: URIUtils.java    From yue-library with Apache License 2.0 2 votes vote down vote up
/**
 * URI编码
 * @param source 要编码的字符串
 * @return 编码后的字符串
 */
public static String encode(String source) {
	return UriUtils.encode(source, DEFAULT_ENCODING);
}