List of usage examples for org.apache.commons.lang StringUtils endsWith
public static boolean endsWith(String str, String suffix)
Check if a String ends with a specified suffix.
From source file:net.ymate.platform.mvc.web.support.RequestMappingParser.java
/** * @param partStr ?//from www . java2 s . c om * @return '/' */ protected String fixMappingPart(String partStr) { partStr = StringUtils.trimToEmpty(partStr); if (StringUtils.startsWith(partStr, "/")) { partStr = StringUtils.substringAfter(partStr, "/"); } if (StringUtils.endsWith(partStr, "/")) { partStr = StringUtils.substringBeforeLast(partStr, "/"); } return partStr; }
From source file:net.ymate.platform.webmvc.support.RequestMappingParser.java
/** * @param partStr ?/*from ww w .j a va 2s.com*/ * @return '/' */ private String __doFixMappingPart(String partStr) { partStr = StringUtils.trimToEmpty(partStr); if (StringUtils.startsWith(partStr, "/")) { partStr = StringUtils.substringAfter(partStr, "/"); } if (StringUtils.endsWith(partStr, "/")) { partStr = StringUtils.substringBeforeLast(partStr, "/"); } return partStr; }
From source file:org.apache.archiva.admin.repository.remote.DefaultRemoteRepositoryAdmin.java
protected String calculateIndexRemoteUrl(RemoteRepository remoteRepository) { if (StringUtils.startsWith(remoteRepository.getRemoteIndexUrl(), "http")) { String baseUrl = remoteRepository.getRemoteIndexUrl(); return baseUrl.endsWith("/") ? StringUtils.substringBeforeLast(baseUrl, "/") : baseUrl; }/*from w ww. ja v a 2s . c o m*/ String baseUrl = StringUtils.endsWith(remoteRepository.getUrl(), "/") ? StringUtils.substringBeforeLast(remoteRepository.getUrl(), "/") : remoteRepository.getUrl(); baseUrl = StringUtils.isEmpty(remoteRepository.getRemoteIndexUrl()) ? baseUrl + "/.index" : baseUrl + "/" + remoteRepository.getRemoteIndexUrl(); return baseUrl; }
From source file:org.apache.archiva.dependency.tree.maven2.Maven3DependencyTreeBuilder.java
private ManagedRepository findArtifactInRepositories(List<String> repositoryIds, Artifact projectArtifact) throws RepositoryAdminException { for (String repoId : repositoryIds) { ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repoId); File repoDir = new File(managedRepository.getLocation()); File file = pathTranslator.toFile(repoDir, projectArtifact.getGroupId(), projectArtifact.getArtifactId(), projectArtifact.getBaseVersion(), projectArtifact.getArtifactId() + "-" + projectArtifact.getVersion() + ".pom"); if (file.exists()) { return managedRepository; }/*w w w .j a v a 2 s . c o m*/ // try with snapshot version if (StringUtils.endsWith(projectArtifact.getBaseVersion(), VersionUtil.SNAPSHOT)) { File metadataFile = new File(file.getParent(), MetadataTools.MAVEN_METADATA); if (metadataFile.exists()) { try { ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader .read(metadataFile); int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber(); String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp(); // rebuild file name with timestamped version and build number String timeStampFileName = new StringBuilder(projectArtifact.getArtifactId()).append('-') .append(StringUtils.remove(projectArtifact.getBaseVersion(), "-" + VersionUtil.SNAPSHOT)) .append('-').append(timeStamp).append('-').append(Integer.toString(buildNumber)) .append(".pom").toString(); File timeStampFile = new File(file.getParent(), timeStampFileName); log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath()); if (timeStampFile.exists()) { return managedRepository; } } catch (XMLException e) { log.warn("skip fail to find timestamped snapshot pom: {}", e.getMessage()); } } } } return null; }
From source file:org.apache.archiva.metadata.repository.cassandra.CassandraMetadataRepository.java
@Override public Collection<String> getNamespaces(final String repoId, final String namespaceId) throws MetadataResolutionException { QueryResult<OrderedRows<String, String, String>> result = HFactory // .createRangeSlicesQuery(keyspace, ss, ss, ss) // .setColumnFamily(cassandraArchivaManager.getNamespaceFamilyName()) // .setColumnNames(NAME.toString()) // .addEqualsExpression(REPOSITORY_NAME.toString(), repoId) // .execute();//from www .j av a 2 s . c o m List<String> namespaces = new ArrayList<>(result.get().getCount()); for (Row<String, String, String> row : result.get()) { String currentNamespace = getStringValue(row.getColumnSlice(), NAME.toString()); if (StringUtils.startsWith(currentNamespace, namespaceId) // && (StringUtils.length(currentNamespace) > StringUtils.length(namespaceId))) { // store after namespaceId '.' but before next '.' // call org namespace org.apache.maven.shared -> stored apache String calledNamespace = StringUtils.endsWith(namespaceId, ".") ? namespaceId : namespaceId + "."; String storedNamespace = StringUtils.substringAfter(currentNamespace, calledNamespace); storedNamespace = StringUtils.substringBefore(storedNamespace, "."); namespaces.add(storedNamespace); } } return namespaces; }
From source file:org.apache.archiva.metadata.repository.storage.maven2.Maven2RepositoryStorage.java
@Override public String getFilePathWithVersion(final String requestPath, ManagedRepositoryContent managedRepositoryContent) throws XMLException, RelocationException { if (StringUtils.endsWith(requestPath, METADATA_FILENAME)) { return getFilePath(requestPath, managedRepositoryContent.getRepository()); }/*from w w w . ja v a 2s .co m*/ String filePath = getFilePath(requestPath, managedRepositoryContent.getRepository()); ArtifactReference artifactReference = null; try { artifactReference = pathParser.toArtifactReference(filePath); } catch (LayoutException e) { return filePath; } if (StringUtils.endsWith(artifactReference.getVersion(), VersionUtil.SNAPSHOT)) { // read maven metadata to get last timestamp File metadataDir = new File(managedRepositoryContent.getRepoRoot(), filePath).getParentFile(); if (!metadataDir.exists()) { return filePath; } File metadataFile = new File(metadataDir, METADATA_FILENAME); if (!metadataFile.exists()) { return filePath; } ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader.read(metadataFile); int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber(); String timestamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp(); // MRM-1846 if (buildNumber < 1 && timestamp == null) { return filePath; } // org/apache/archiva/archiva-checksum/1.4-M4-SNAPSHOT/archiva-checksum-1.4-M4-SNAPSHOT.jar // -> archiva-checksum-1.4-M4-20130425.081822-1.jar filePath = StringUtils.replace(filePath, // artifactReference.getArtifactId() // + "-" + artifactReference.getVersion(), // artifactReference.getArtifactId() // + "-" + StringUtils.remove(artifactReference.getVersion(), "-" + VersionUtil.SNAPSHOT) // + "-" + timestamp // + "-" + buildNumber); throw new RelocationException( "/repository/" + managedRepositoryContent.getRepository().getId() + (StringUtils.startsWith(filePath, "/") ? "" : "/") + filePath, RelocationException.RelocationType.TEMPORARY); } return filePath; }
From source file:org.apache.archiva.rest.services.DefaultBrowseService.java
@Override public ProjectVersionMetadata getProjectVersionMetadata(String groupId, String artifactId, String repositoryId) throws ArchivaRestServiceException { List<String> selectedRepos = getSelectedRepos(repositoryId); RepositorySession repositorySession = null; try {/*from w w w . j a va 2 s . co m*/ Collection<String> projectVersions = getVersions(selectedRepos, groupId, artifactId); repositorySession = repositorySessionFactory.createSession(); MetadataResolver metadataResolver = repositorySession.getResolver(); ProjectVersionMetadata sharedModel = new ProjectVersionMetadata(); MavenProjectFacet mavenFacet = new MavenProjectFacet(); mavenFacet.setGroupId(groupId); mavenFacet.setArtifactId(artifactId); sharedModel.addFacet(mavenFacet); boolean isFirstVersion = true; for (String version : projectVersions) { ProjectVersionMetadata versionMetadata = null; for (String repoId : selectedRepos) { if (versionMetadata == null || versionMetadata.isIncomplete()) { try { ProjectVersionMetadata projectVersionMetadataResolved = null; boolean useCache = !StringUtils.endsWith(version, VersionUtil.SNAPSHOT); String cacheKey = null; boolean cacheToUpdate = false; // FIXME a bit maven centric!!! // not a snapshot so get it from cache if (useCache) { cacheKey = repoId + groupId + artifactId + version; projectVersionMetadataResolved = versionMetadataCache.get(cacheKey); } if (useCache && projectVersionMetadataResolved != null) { versionMetadata = projectVersionMetadataResolved; } else { projectVersionMetadataResolved = metadataResolver.resolveProjectVersion( repositorySession, repoId, groupId, artifactId, version); versionMetadata = projectVersionMetadataResolved; cacheToUpdate = true; } if (useCache && cacheToUpdate) { versionMetadataCache.put(cacheKey, projectVersionMetadataResolved); } } catch (MetadataResolutionException e) { log.error("Skipping invalid metadata while compiling shared model for " + groupId + ":" + artifactId + " in repo " + repoId + ": " + e.getMessage()); } } } if (versionMetadata == null) { continue; } if (isFirstVersion) { sharedModel = versionMetadata; sharedModel.setId(null); } else { MavenProjectFacet versionMetadataMavenFacet = (MavenProjectFacet) versionMetadata .getFacet(MavenProjectFacet.FACET_ID); if (versionMetadataMavenFacet != null) { if (mavenFacet.getPackaging() != null // && !StringUtils.equalsIgnoreCase(mavenFacet.getPackaging(), versionMetadataMavenFacet.getPackaging())) { mavenFacet.setPackaging(null); } } if (StringUtils.isEmpty(sharedModel.getName()) // && !StringUtils.isEmpty(versionMetadata.getName())) { sharedModel.setName(versionMetadata.getName()); } if (sharedModel.getDescription() != null // && !StringUtils.equalsIgnoreCase(sharedModel.getDescription(), versionMetadata.getDescription())) { sharedModel.setDescription(StringUtils.isNotEmpty(versionMetadata.getDescription()) ? versionMetadata.getDescription() : ""); } if (sharedModel.getIssueManagement() != null // && versionMetadata.getIssueManagement() != null // && !StringUtils.equalsIgnoreCase(sharedModel.getIssueManagement().getUrl(), versionMetadata.getIssueManagement().getUrl())) { sharedModel.setIssueManagement(versionMetadata.getIssueManagement()); } if (sharedModel.getCiManagement() != null // && versionMetadata.getCiManagement() != null // && !StringUtils.equalsIgnoreCase(sharedModel.getCiManagement().getUrl(), versionMetadata.getCiManagement().getUrl())) { sharedModel.setCiManagement(versionMetadata.getCiManagement()); } if (sharedModel.getOrganization() != null // && versionMetadata.getOrganization() != null // && !StringUtils.equalsIgnoreCase(sharedModel.getOrganization().getName(), versionMetadata.getOrganization().getName())) { sharedModel.setOrganization(versionMetadata.getOrganization()); } if (sharedModel.getUrl() != null // && !StringUtils.equalsIgnoreCase(sharedModel.getUrl(), versionMetadata.getUrl())) { sharedModel.setUrl(versionMetadata.getUrl()); } } isFirstVersion = false; } return sharedModel; } catch (MetadataResolutionException e) { throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); } finally { if (repositorySession != null) { repositorySession.close(); } } }
From source file:org.apache.archiva.rest.services.DefaultBrowseService.java
@Override public Boolean artifactAvailable(String groupId, String artifactId, String version, String classifier, String repositoryId) throws ArchivaRestServiceException { List<String> selectedRepos = getSelectedRepos(repositoryId); boolean snapshot = VersionUtil.isSnapshot(version); try {// ww w . ja v a 2s. c o m for (String repoId : selectedRepos) { ManagedRepository managedRepository = managedRepositoryAdmin.getManagedRepository(repoId); if ((snapshot && !managedRepository.isSnapshots()) || (!snapshot && managedRepository.isSnapshots())) { continue; } ManagedRepositoryContent managedRepositoryContent = repositoryContentFactory .getManagedRepositoryContent(repoId); // FIXME default to jar which can be wrong for war zip etc.... ArchivaArtifact archivaArtifact = new ArchivaArtifact(groupId, artifactId, version, StringUtils.isEmpty(classifier) ? "" : classifier, "jar", repoId); File file = managedRepositoryContent.toFile(archivaArtifact); if (file != null && file.exists()) { return true; } // in case of SNAPSHOT we can have timestamped version locally ! if (StringUtils.endsWith(version, VersionUtil.SNAPSHOT)) { File metadataFile = new File(file.getParent(), MetadataTools.MAVEN_METADATA); if (metadataFile.exists()) { try { ArchivaRepositoryMetadata archivaRepositoryMetadata = MavenMetadataReader .read(metadataFile); int buildNumber = archivaRepositoryMetadata.getSnapshotVersion().getBuildNumber(); String timeStamp = archivaRepositoryMetadata.getSnapshotVersion().getTimestamp(); // rebuild file name with timestamped version and build number String timeStampFileName = new StringBuilder(artifactId).append('-') // .append(StringUtils.remove(version, "-" + VersionUtil.SNAPSHOT)) // .append('-').append(timeStamp) // .append('-').append(Integer.toString(buildNumber)) // .append((StringUtils.isEmpty(classifier) ? "" : "-" + classifier)) // .append(".jar").toString(); File timeStampFile = new File(file.getParent(), timeStampFileName); log.debug("try to find timestamped snapshot version file: {}", timeStampFile.getPath()); if (timeStampFile.exists()) { return true; } } catch (XMLException e) { log.warn("skip fail to find timestamped snapshot file: {}", e.getMessage()); } } } String path = managedRepositoryContent.toPath(archivaArtifact); file = connectors.fetchFromProxies(managedRepositoryContent, path); if (file != null && file.exists()) { // download pom now String pomPath = StringUtils.substringBeforeLast(path, ".jar") + ".pom"; connectors.fetchFromProxies(managedRepositoryContent, pomPath); return true; } } } catch (RepositoryAdminException e) { log.error(e.getMessage(), e); throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); } catch (RepositoryException e) { log.error(e.getMessage(), e); throw new ArchivaRestServiceException(e.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e); } return false; }
From source file:org.apache.archiva.rest.services.DefaultBrowseService.java
protected List<ArtifactContentEntry> readFileEntries(File file, String filterPath, String repoId) throws IOException { Map<String, ArtifactContentEntry> artifactContentEntryMap = new HashMap<>(); int filterDepth = StringUtils.countMatches(filterPath, "/"); /*if ( filterDepth == 0 ) {/* w ww .j a va 2s. c om*/ filterDepth = 1; }*/ JarFile jarFile = new JarFile(file); try { Enumeration<JarEntry> jarEntryEnumeration = jarFile.entries(); while (jarEntryEnumeration.hasMoreElements()) { JarEntry currentEntry = jarEntryEnumeration.nextElement(); String cleanedEntryName = StringUtils.endsWith(currentEntry.getName(), "/") ? // StringUtils.substringBeforeLast(currentEntry.getName(), "/") : currentEntry.getName(); String entryRootPath = getRootPath(cleanedEntryName); int depth = StringUtils.countMatches(cleanedEntryName, "/"); if (StringUtils.isEmpty(filterPath) // && !artifactContentEntryMap.containsKey(entryRootPath) // && depth == filterDepth) { artifactContentEntryMap.put(entryRootPath, new ArtifactContentEntry(entryRootPath, !currentEntry.isDirectory(), depth, repoId)); } else { if (StringUtils.startsWith(cleanedEntryName, filterPath) // && (depth == filterDepth || (!currentEntry.isDirectory() && depth == filterDepth))) { artifactContentEntryMap.put(cleanedEntryName, new ArtifactContentEntry(cleanedEntryName, !currentEntry.isDirectory(), depth, repoId)); } } } if (StringUtils.isNotEmpty(filterPath)) { Map<String, ArtifactContentEntry> filteredArtifactContentEntryMap = new HashMap<>(); for (Map.Entry<String, ArtifactContentEntry> entry : artifactContentEntryMap.entrySet()) { filteredArtifactContentEntryMap.put(entry.getKey(), entry.getValue()); } List<ArtifactContentEntry> sorted = getSmallerDepthEntries(filteredArtifactContentEntryMap); if (sorted == null) { return Collections.emptyList(); } Collections.sort(sorted, ArtifactContentEntryComparator.INSTANCE); return sorted; } } finally { if (jarFile != null) { jarFile.close(); } } List<ArtifactContentEntry> sorted = new ArrayList<>(artifactContentEntryMap.values()); Collections.sort(sorted, ArtifactContentEntryComparator.INSTANCE); return sorted; }
From source file:org.apache.archiva.web.docs.RestDocsServlet.java
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { logger.debug("docs request to path: {}", req.getPathInfo()); String path = StringUtils.removeStart(req.getPathInfo(), "/"); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(path); if (StringUtils.endsWith(path, ".xsd")) { StringEscapeUtils.escapeXml(resp.getWriter(), IOUtils.toString(is)); //IOUtils.copy( is, resp.getOutputStream() ); return;//from w ww . j a v a 2 s.c o m } String startPath = StringUtils.substringBefore(path, "/"); // replace all links !! Document document = Jsoup.parse(is, "UTF-8", ""); Element body = document.body().child(0); Elements links = body.select("a[href]"); for (Element link : links) { link.attr("href", "#" + startPath + "/" + link.attr("href")); } Elements datalinks = body.select("[data-href]"); for (Element link : datalinks) { link.attr("data-href", "#" + startPath + "/" + link.attr("data-href")); } Elements codes = body.select("code"); for (Element code : codes) { code.attr("class", code.attr("class") + " nice-code"); } //default generated enunciate use h1/h2/h3 which is quite big so transform to h3/h4/h5 Elements headers = body.select("h1"); for (Element header : headers) { header.tagName("h3"); } headers = body.select("h2"); for (Element header : headers) { header.tagName("h4"); } headers = body.select("h3"); for (Element header : headers) { header.tagName("h5"); } Document res = new Document(""); res.appendChild(body.select("div[id=main]").first()); Elements scripts = body.select("script"); for (Element script : scripts) { res.appendChild(script); } resp.getOutputStream().write(res.outerHtml().getBytes()); }