use of org.apache.commons.lang3.StringUtils.substringBeforeLast in project cxf by apache.
the class OpenApiCustomizer method customize.
public OpenAPIConfiguration customize(final OpenAPIConfiguration configuration) {
if (configuration == null) {
return configuration;
}
if (dynamicBasePath) {
final MessageContext ctx = createMessageContext();
// If the JAX-RS application with custom path is defined, it might be present twice, in the
// request URI as well as in each resource operation URI. To properly represent server URL,
// the application path should be removed from it.
final String url = StringUtils.removeEnd(StringUtils.substringBeforeLast(ctx.getUriInfo().getRequestUri().toString(), "/"), applicationPath);
final Collection<Server> servers = configuration.getOpenAPI().getServers();
if (servers == null || servers.stream().noneMatch(s -> s.getUrl().equalsIgnoreCase(url))) {
configuration.getOpenAPI().setServers(Collections.singletonList(new Server().url(url)));
}
}
return configuration;
}
use of org.apache.commons.lang3.StringUtils.substringBeforeLast in project cxf by apache.
the class Swagger2Customizer method customize.
public Swagger customize(Swagger data) {
if (dynamicBasePath) {
MessageContext ctx = createMessageContext();
String currentBasePath = StringUtils.substringBeforeLast(ctx.getHttpServletRequest().getRequestURI(), "/");
data.setBasePath(currentBasePath);
if (data.getHost() == null) {
data.setHost(beanConfig.getHost());
}
if (data.getInfo() == null) {
data.setInfo(beanConfig.getInfo());
}
if (beanConfig.getSwagger() != null && beanConfig.getSwagger().getSecurityDefinitions() != null && data.getSecurityDefinitions() == null) {
data.setSecurityDefinitions(beanConfig.getSwagger().getSecurityDefinitions());
}
}
if (replaceTags || javadocProvider != null) {
Map<String, ClassResourceInfo> operations = new HashMap<>();
Map<Pair<String, String>, OperationResourceInfo> methods = new HashMap<>();
for (ClassResourceInfo cri : cris) {
for (OperationResourceInfo ori : cri.getMethodDispatcher().getOperationResourceInfos()) {
String normalizedPath = getNormalizedPath(cri.getURITemplate().getValue(), ori.getURITemplate().getValue());
operations.put(normalizedPath, cri);
methods.put(ImmutablePair.of(ori.getHttpMethod(), normalizedPath), ori);
}
}
if (replaceTags && data.getTags() != null) {
data.getTags().clear();
}
for (final Map.Entry<String, Path> entry : data.getPaths().entrySet()) {
Tag tag = null;
if (replaceTags && operations.containsKey(entry.getKey())) {
ClassResourceInfo cri = operations.get(entry.getKey());
tag = new Tag();
tag.setName(cri.getURITemplate().getValue().replaceAll("/", "_"));
if (javadocProvider != null) {
tag.setDescription(javadocProvider.getClassDoc(cri));
}
data.addTag(tag);
}
for (Map.Entry<HttpMethod, Operation> subentry : entry.getValue().getOperationMap().entrySet()) {
if (replaceTags && tag != null) {
subentry.getValue().setTags(Collections.singletonList(tag.getName()));
}
Pair<String, String> key = ImmutablePair.of(subentry.getKey().name(), entry.getKey());
if (methods.containsKey(key) && javadocProvider != null) {
OperationResourceInfo ori = methods.get(key);
subentry.getValue().setSummary(javadocProvider.getMethodDoc(ori));
for (int i = 0; i < subentry.getValue().getParameters().size(); i++) {
subentry.getValue().getParameters().get(i).setDescription(javadocProvider.getMethodParameterDoc(ori, i));
}
addParameters(subentry.getValue().getParameters());
if (subentry.getValue().getResponses() != null && !subentry.getValue().getResponses().isEmpty()) {
subentry.getValue().getResponses().entrySet().iterator().next().getValue().setDescription(javadocProvider.getMethodResponseDoc(ori));
}
}
}
}
}
if (replaceTags && data.getTags() != null) {
Collections.sort(data.getTags(), new Comparator<Tag>() {
@Override
public int compare(final Tag tag1, final Tag tag2) {
return tag1.getName().compareTo(tag2.getName());
}
});
}
applyDefaultVersion(data);
return data;
}
use of org.apache.commons.lang3.StringUtils.substringBeforeLast in project cas by apereo.
the class ConfigurationMetadataGenerator method processHints.
private static Set<ConfigurationMetadataHint> processHints(final Collection<ConfigurationMetadataProperty> props, final Collection<ConfigurationMetadataProperty> groups) {
var hints = new LinkedHashSet<ConfigurationMetadataHint>(0);
val allValidProps = props.stream().filter(p -> p.getDeprecation() == null || !Deprecation.Level.ERROR.equals(p.getDeprecation().getLevel())).collect(Collectors.toList());
for (val entry : allValidProps) {
try {
val propName = StringUtils.substringAfterLast(entry.getName(), ".");
val groupName = StringUtils.substringBeforeLast(entry.getName(), ".");
val grp = groups.stream().filter(g -> g.getName().equalsIgnoreCase(groupName)).findFirst().orElseThrow(() -> new IllegalArgumentException("Cant locate group " + groupName));
val matcher = PATTERN_GENERICS.matcher(grp.getType());
val className = matcher.find() ? matcher.group(1) : grp.getType();
val clazz = ClassUtils.getClass(className);
val hint = new ConfigurationMetadataHint();
hint.setName(entry.getName());
val annotation = Arrays.stream(clazz.getAnnotations()).filter(a -> a.annotationType().equals(RequiresModule.class)).findFirst().map(RequiresModule.class::cast).orElseThrow(() -> new RuntimeException(clazz.getCanonicalName() + " is missing @RequiresModule"));
val valueHint = new ValueHint();
valueHint.setValue(toJson(Map.of("module", annotation.name(), "automated", annotation.automated())));
valueHint.setDescription(RequiresModule.class.getName());
hint.getValues().add(valueHint);
val grpHint = new ValueHint();
grpHint.setValue(toJson(Map.of("owner", clazz.getCanonicalName())));
grpHint.setDescription(PropertyOwner.class.getName());
hint.getValues().add(grpHint);
val names = RelaxedPropertyNames.forCamelCase(propName);
names.getValues().forEach(Unchecked.consumer(name -> {
val f = ReflectionUtils.findField(clazz, name);
if (f != null && f.isAnnotationPresent(RequiredProperty.class)) {
val propertyHint = new ValueHint();
propertyHint.setValue(toJson(Map.of("owner", clazz.getName())));
propertyHint.setDescription(RequiredProperty.class.getName());
hint.getValues().add(propertyHint);
}
if (f != null && f.isAnnotationPresent(DurationCapable.class)) {
val propertyHint = new ValueHint();
propertyHint.setDescription(DurationCapable.class.getName());
propertyHint.setValue(toJson(List.of(DurationCapable.class.getName())));
hint.getValues().add(propertyHint);
}
if (f != null && f.isAnnotationPresent(ExpressionLanguageCapable.class)) {
val propertyHint = new ValueHint();
propertyHint.setDescription(ExpressionLanguageCapable.class.getName());
propertyHint.setValue(toJson(List.of(ExpressionLanguageCapable.class.getName())));
hint.getValues().add(propertyHint);
}
}));
if (!hint.getValues().isEmpty()) {
hints.add(hint);
}
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
}
}
return hints;
}
use of org.apache.commons.lang3.StringUtils.substringBeforeLast in project simplelocalize-cli by simplelocalize.
the class FileListReader method findFilesToUpload.
public List<FileToUpload> findFilesToUpload(String uploadPath) throws IOException {
List<FileToUpload> output = new ArrayList<>();
String beforeTemplatePart = getParentDirectory(uploadPath);
Path parentDir = Path.of(beforeTemplatePart);
boolean exists = Files.exists(parentDir);
if (!exists) {
String parentDirectory = StringUtils.substringBeforeLast(beforeTemplatePart, File.separator);
parentDir = Path.of(parentDirectory);
}
try (Stream<Path> foundFilesStream = Files.walk(parentDir, 6)) {
AntPathMatcher antPathMatcher = new AntPathMatcher();
String uploadPathPattern = uploadPath.replace(LANGUAGE_TEMPLATE_KEY, "**").replace(NAMESPACE_TEMPLATE_KEY, "**");
var foundFiles = foundFilesStream.filter(Files::isRegularFile).filter(path -> antPathMatcher.matches(uploadPathPattern, path.toString())).collect(Collectors.toList());
for (Path foundFile : foundFiles) {
String languageKey = extractTemplateValue(uploadPath, foundFile, LANGUAGE_TEMPLATE_KEY);
String pathWithLanguage = uploadPath.replace(LANGUAGE_TEMPLATE_KEY, languageKey);
String namespace = extractTemplateValue(pathWithLanguage, foundFile, NAMESPACE_TEMPLATE_KEY);
if (StringUtils.isNotBlank(namespace)) {
String pathWithNamespace = uploadPath.replace(NAMESPACE_TEMPLATE_KEY, namespace);
languageKey = extractTemplateValue(pathWithNamespace, foundFile, LANGUAGE_TEMPLATE_KEY);
}
FileToUpload fileToUpload = FileToUpload.FileToUploadBuilder.aFileToUpload().withLanguage(StringUtils.trimToNull(languageKey)).withNamespace(StringUtils.trimToNull(namespace)).withPath(foundFile).build();
output.add(fileToUpload);
}
return output;
}
}
use of org.apache.commons.lang3.StringUtils.substringBeforeLast in project opencast by opencast.
the class CanvasUserRoleProvider method getCanvasSiteRolesByCurrentUser.
/**
* Get all sites id taught by current user.
* Only list available site roles for current user.
*/
private List<String> getCanvasSiteRolesByCurrentUser(String query, boolean exact) {
User user = securityService.getUser();
if (exact) {
Set<String> roles = user.getRoles().stream().map(Role::getName).collect(Collectors.toSet());
if (roles.contains(query)) {
return Collections.singletonList(query);
} else {
return Collections.emptyList();
}
}
final String finalQuery = StringUtils.chop(query);
return user.getRoles().stream().map(Role::getName).filter(roleName -> roleName.endsWith("_" + LTI_INSTRUCTOR_ROLE) || roleName.endsWith("_" + LTI_LEARNER_ROLE)).map(roleName -> StringUtils.substringBeforeLast(roleName, "_")).distinct().map(site -> Arrays.asList(site + "_" + LTI_LEARNER_ROLE, site + "_" + LTI_INSTRUCTOR_ROLE)).flatMap(siteRoles -> siteRoles.stream()).filter(roleName -> roleName.startsWith(finalQuery)).collect(Collectors.toList());
}
Aggregations