Java Code Examples for org.springframework.util.StringUtils#startsWithIgnoreCase()

The following examples show how to use org.springframework.util.StringUtils#startsWithIgnoreCase() . 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: DtsRemoteInterceptor.java    From dts with Apache License 2.0 6 votes vote down vote up
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    Enumeration<String> headerNames = request.getHeaderNames();
    if (headerNames != null) {
        while (headerNames.hasMoreElements()) {
            String headerName = headerNames.nextElement();
            if (StringUtils.startsWithIgnoreCase(headerName, CONTEXT_HEADER_PARENT)) {
                String value = request.getHeader(headerName);
                String key = StringUtils.replace(headerName, CONTEXT_HEADER_PARENT, "");
                SpringCloudDtsContext.getContext().setAttachment(key.toUpperCase(), value);
            }
        }
    }
    return true;
}
 
Example 2
Source File: AlarmValidator.java    From cerebro with GNU Affero General Public License v3.0 6 votes vote down vote up
public void validateAlarm(final Alarm alarm) throws CerebroException {
    // Validate required fields
    if (StringUtils.isEmpty(alarm.getName())) {
        throw new CerebroException(ErrorCode.ALARM_INVALID, "Alarm name is required.");
    }

    if (StringUtils.isEmpty(alarm.getTarget()) || StringUtils.startsWithIgnoreCase(alarm.getTarget(), "*")) {
        throw new CerebroException(ErrorCode.ALARM_INVALID, "Alarm target is required.");
    }

    if (StringUtils.isEmpty(alarm.getWarn())) {
        throw new CerebroException(ErrorCode.ALARM_INVALID, "Alarm warning threshold is required.");
    }

    if (StringUtils.isEmpty(alarm.getError())) {
        throw new CerebroException(ErrorCode.ALARM_INVALID, "Alarm error threshold is required.");
    }
}
 
Example 3
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
/**
 * Deletes local branches if corresponding remote branch was removed.
 * @param trackingRefUpdates list of tracking ref updates
 * @param git git instance
 * @return list of deleted branches
 */
private Collection<String> deleteUntrackedLocalBranches(
		Collection<TrackingRefUpdate> trackingRefUpdates, Git git) {
	if (CollectionUtils.isEmpty(trackingRefUpdates)) {
		return Collections.emptyList();
	}

	Collection<String> branchesToDelete = new ArrayList<>();
	for (TrackingRefUpdate trackingRefUpdate : trackingRefUpdates) {
		ReceiveCommand receiveCommand = trackingRefUpdate.asReceiveCommand();
		if (receiveCommand.getType() == DELETE) {
			String localRefName = trackingRefUpdate.getLocalName();
			if (StringUtils.startsWithIgnoreCase(localRefName,
					LOCAL_BRANCH_REF_PREFIX)) {
				String localBranchName = localRefName.substring(
						LOCAL_BRANCH_REF_PREFIX.length(), localRefName.length());
				branchesToDelete.add(localBranchName);
			}
		}
	}

	if (CollectionUtils.isEmpty(branchesToDelete)) {
		return Collections.emptyList();
	}

	try {
		// make sure that deleted branch not a current one
		checkout(git, this.defaultLabel);
		return deleteBranches(git, branchesToDelete);
	}
	catch (Exception ex) {
		String message = format("Failed to delete %s branches.", branchesToDelete);
		warn(message, ex);
		return Collections.emptyList();
	}
}
 
Example 4
Source File: MockCookie.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Factory method that parses the value of a "Set-Cookie" header.
 * @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
 * @return the created cookie
 */
public static MockCookie parse(String setCookieHeader) {
	Assert.notNull(setCookieHeader, "Set-Cookie header must not be null");
	String[] cookieParts = setCookieHeader.split("\\s*=\\s*", 2);
	Assert.isTrue(cookieParts.length == 2, () -> "Invalid Set-Cookie header '" + setCookieHeader + "'");

	String name = cookieParts[0];
	String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2);
	String value = valueAndAttributes[0];
	String[] attributes =
			(valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0]);

	MockCookie cookie = new MockCookie(name, value);
	for (String attribute : attributes) {
		if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
			cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
			cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
			cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
			cookie.setSecure(true);
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
			cookie.setHttpOnly(true);
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
			cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
		}
	}
	return cookie;
}
 
Example 5
Source File: MockCookie.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Factory method that parses the value of a "Set-Cookie" header.
 * @param setCookieHeader the "Set-Cookie" value; never {@code null} or empty
 * @return the created cookie
 */
public static MockCookie parse(String setCookieHeader) {
	Assert.notNull(setCookieHeader, "Set-Cookie header must not be null");
	String[] cookieParts = setCookieHeader.split("\\s*=\\s*", 2);
	Assert.isTrue(cookieParts.length == 2, () -> "Invalid Set-Cookie header '" + setCookieHeader + "'");

	String name = cookieParts[0];
	String[] valueAndAttributes = cookieParts[1].split("\\s*;\\s*", 2);
	String value = valueAndAttributes[0];
	String[] attributes =
			(valueAndAttributes.length > 1 ? valueAndAttributes[1].split("\\s*;\\s*") : new String[0]);

	MockCookie cookie = new MockCookie(name, value);
	for (String attribute : attributes) {
		if (StringUtils.startsWithIgnoreCase(attribute, "Domain")) {
			cookie.setDomain(extractAttributeValue(attribute, setCookieHeader));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Max-Age")) {
			cookie.setMaxAge(Integer.parseInt(extractAttributeValue(attribute, setCookieHeader)));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Path")) {
			cookie.setPath(extractAttributeValue(attribute, setCookieHeader));
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "Secure")) {
			cookie.setSecure(true);
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "HttpOnly")) {
			cookie.setHttpOnly(true);
		}
		else if (StringUtils.startsWithIgnoreCase(attribute, "SameSite")) {
			cookie.setSameSite(extractAttributeValue(attribute, setCookieHeader));
		}
	}
	return cookie;
}
 
Example 6
Source File: TemplateHelper.java    From citrus-simulator with Apache License 2.0 5 votes vote down vote up
/**
 * Gets a classpath file resource from base template package.
 *
 * @param resourcePath      the relative path to the resource, including the resource name
 * @param resourceExtension the resource extension (e.g. '.xml')
 * @return the classpath resource
 */
public Resource getFileResource(String resourcePath, String resourceExtension) {
    String adaptedFileExtension = resourceExtension;
    if (StringUtils.hasLength(resourceExtension) && !StringUtils.startsWithIgnoreCase(resourceExtension, ".")) {
        adaptedFileExtension = "." + resourceExtension;
    }
    return new ClassPathResource(basePath + resourcePath + adaptedFileExtension);
}
 
Example 7
Source File: PutAwareStandardServletMultiPartResolver.java    From flowable-engine with Apache License 2.0 5 votes vote down vote up
/**
 * Utility method that determines whether the request contains multipart content.
 *
 * @param request
 *     The servlet request to be evaluated. Must be non-null.
 * @return <code>true</code> if the request is multipart; {@code false} otherwise.
 * @see org.apache.commons.fileupload.servlet.ServletFileUpload#isMultipartContent(HttpServletRequest)
 */
public static final boolean isMultipartContent(HttpServletRequest request) {
    final String method = request.getMethod().toLowerCase();
    if (!method.equalsIgnoreCase("post") && !method.equalsIgnoreCase("put")) {
        return false;
    }

    String contentType = request.getContentType();
    return StringUtils.startsWithIgnoreCase(contentType, "multipart/");
}
 
Example 8
Source File: CommonController.java    From halo with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Handles custom exception, like HaloException.
 *
 * @param request http servlet request must not be null
 */
private void handleCustomException(@NonNull HttpServletRequest request) {
    Assert.notNull(request, "Http servlet request must not be null");

    Object throwableObject = request.getAttribute("javax.servlet.error.exception");
    if (throwableObject == null) {
        return;
    }

    Throwable throwable = (Throwable) throwableObject;

    if (throwable instanceof NestedServletException) {
        log.error("Captured an exception", throwable);
        Throwable rootCause = ((NestedServletException) throwable).getRootCause();
        if (rootCause instanceof AbstractHaloException) {
            AbstractHaloException haloException = (AbstractHaloException) rootCause;
            request.setAttribute("javax.servlet.error.status_code", haloException.getStatus().value());
            request.setAttribute("javax.servlet.error.exception", rootCause);
            request.setAttribute("javax.servlet.error.message", haloException.getMessage());
        }
    } else if (StringUtils.startsWithIgnoreCase(throwable.getMessage(), COULD_NOT_RESOLVE_VIEW_WITH_NAME_PREFIX)) {
        log.debug("Captured an exception", throwable);
        request.setAttribute("javax.servlet.error.status_code", HttpStatus.NOT_FOUND.value());

        NotFoundException viewNotFound = new NotFoundException("该路径没有对应的模板");
        request.setAttribute("javax.servlet.error.exception", viewNotFound);
        request.setAttribute("javax.servlet.error.message", viewNotFound.getMessage());
    }

}
 
Example 9
Source File: MissingSpringCloudRegistryConfigPropertyCondition.java    From spring-cloud-alibaba with Apache License 2.0 5 votes vote down vote up
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context,
		AnnotatedTypeMetadata metadata) {
	ConfigurableEnvironment environment = (ConfigurableEnvironment) context
			.getEnvironment();

	String protocol = environment.getProperty("dubbo.registry.protocol");

	if (PROTOCOL.equals(protocol)) {
		return ConditionOutcome.noMatch(
				"'spring-cloud' protocol was found from 'dubbo.registry.protocol'");
	}

	String address = environment.getProperty("dubbo.registry.address");

	if (StringUtils.startsWithIgnoreCase(address, PROTOCOL)) {
		return ConditionOutcome.noMatch(
				"'spring-cloud' protocol was found from 'dubbo.registry.address'");
	}

	Map<String, Object> properties = getSubProperties(
			environment.getPropertySources(), "dubbo.registries.");

	boolean found = properties.entrySet().stream().anyMatch(entry -> {
		String key = entry.getKey();
		String value = String.valueOf(entry.getValue());
		return (key.endsWith(".address") && value.startsWith(PROTOCOL))
				|| (key.endsWith(".protocol") && PROTOCOL.equals(value));

	});

	return found
			? ConditionOutcome.noMatch(
					"'spring-cloud' protocol was found in 'dubbo.registries.*'")
			: ConditionOutcome.match();
}
 
Example 10
Source File: WebRequestDataBinder.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Check if the request is a multipart request (by checking its Content-Type header).
 * @param request request with parameters to bind
 */
private boolean isMultipartRequest(WebRequest request) {
	String contentType = request.getHeader("Content-Type");
	return (contentType != null && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
}
 
Example 11
Source File: WebRequestDataBinder.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Check if the request is a multipart request (by checking its Content-Type header).
 * @param request request with parameters to bind
 */
private boolean isMultipartRequest(WebRequest request) {
	String contentType = request.getHeader("Content-Type");
	return (contentType != null && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
}
 
Example 12
Source File: WebRequestDataBinder.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check if the request is a multipart request (by checking its Content-Type header).
 * @param request request with parameters to bind
 */
private boolean isMultipartRequest(WebRequest request) {
	String contentType = request.getHeader("Content-Type");
	return (contentType != null && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
}
 
Example 13
Source File: WebRequestDataBinder.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Check if the request is a multipart request (by checking its Content-Type header).
 * @param request request with parameters to bind
 */
private boolean isMultipartRequest(WebRequest request) {
	String contentType = request.getHeader("Content-Type");
	return (contentType != null && StringUtils.startsWithIgnoreCase(contentType, "multipart"));
}
 
Example 14
Source File: StandardServletMultipartResolver.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public boolean isMultipart(HttpServletRequest request) {
	return StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/");
}
 
Example 15
Source File: StandardServletMultipartResolver.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public boolean isMultipart(HttpServletRequest request) {
	return StringUtils.startsWithIgnoreCase(request.getContentType(), "multipart/");
}
 
Example 16
Source File: PortletUtils.java    From spring4-understanding with Apache License 2.0 2 votes vote down vote up
/**
 * Check whether the specified path indicates a resource in the protected
 * WEB-INF or META-INF directories.
 * @param path the path to check
 */
private static boolean isProtectedResource(String path) {
	return (StringUtils.startsWithIgnoreCase(path, "/WEB-INF") ||
			StringUtils.startsWithIgnoreCase(path, "/META-INF"));
}
 
Example 17
Source File: UtilsFunctionPackage.java    From beetl2.0 with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * 无视大小写的startsWith判断
 *
 * @param input
 *            测试文本
 * @param prefix
 *            指定前缀
 * @return 无视大小写的startsWith判断
 */
public boolean startsWithIgnoreCase(String input, String prefix) {
	return StringUtils.startsWithIgnoreCase(input, prefix);
}