-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # server/src/main/java/org/eclipse/openvsx/LocalRegistryService.java # server/src/main/resources/ehcache.xml # server/src/test/java/org/eclipse/openvsx/AdminAPITest.java # server/src/test/java/org/eclipse/openvsx/RegistryAPITest.java
- Loading branch information
amvanbaren
committed
Mar 16, 2023
1 parent
a874a24
commit 11d525a
Showing
11 changed files
with
209 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
server/src/main/java/org/eclipse/openvsx/cache/SearchEntryJsonCacheKeyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2022 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.cache; | ||
|
||
import org.eclipse.openvsx.search.ExtensionSearch; | ||
import org.springframework.cache.interceptor.KeyGenerator; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
@Component | ||
public class SearchEntryJsonCacheKeyGenerator implements KeyGenerator { | ||
|
||
@Override | ||
public Object generate(Object target, Method method, Object... params) { | ||
var searchHit = (SearchHit<?>) params[0]; | ||
var includeAllVersions = (boolean) params[1]; | ||
if(searchHit.getContent() instanceof ExtensionSearch) { | ||
var extensionSearch = (ExtensionSearch) searchHit.getContent(); | ||
return generate(extensionSearch.id, includeAllVersions); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
public Object generate(long extensionId, boolean includeAllVersions) { | ||
return "extensionId=" + extensionId + ",includeAllVersions=" + includeAllVersions; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
server/src/main/java/org/eclipse/openvsx/json/SearchEntryService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** ****************************************************************************** | ||
* Copyright (c) 2022 Precies. Software Ltd and others | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
* ****************************************************************************** */ | ||
package org.eclipse.openvsx.json; | ||
|
||
import org.eclipse.openvsx.entities.Extension; | ||
import org.eclipse.openvsx.entities.ExtensionVersion; | ||
import org.eclipse.openvsx.repositories.RepositoryService; | ||
import org.eclipse.openvsx.search.ExtensionSearch; | ||
import org.eclipse.openvsx.search.SearchUtilService; | ||
import org.eclipse.openvsx.storage.StorageUtilService; | ||
import org.eclipse.openvsx.util.UrlUtil; | ||
import org.eclipse.openvsx.util.VersionService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.cache.annotation.Cacheable; | ||
import org.springframework.data.elasticsearch.core.SearchHit; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.persistence.EntityManager; | ||
import javax.transaction.Transactional; | ||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
import static org.eclipse.openvsx.cache.CacheService.CACHE_SEARCH_ENTRY_JSON; | ||
import static org.eclipse.openvsx.cache.CacheService.GENERATOR_SEARCH_ENTRY_JSON; | ||
import static org.eclipse.openvsx.entities.FileResource.DOWNLOAD; | ||
import static org.eclipse.openvsx.entities.FileResource.ICON; | ||
import static org.eclipse.openvsx.util.UrlUtil.createApiUrl; | ||
|
||
@Component | ||
public class SearchEntryService { | ||
|
||
@Autowired | ||
EntityManager entityManager; | ||
|
||
@Autowired | ||
VersionService versions; | ||
|
||
@Autowired | ||
StorageUtilService storageUtil; | ||
|
||
@Autowired | ||
SearchUtilService search; | ||
|
||
@Autowired | ||
RepositoryService repositories; | ||
|
||
@Transactional | ||
@Cacheable(value = CACHE_SEARCH_ENTRY_JSON, keyGenerator = GENERATOR_SEARCH_ENTRY_JSON) | ||
public SearchEntryJson toJson(SearchHit<ExtensionSearch> searchHit, boolean includeAllVersions) { | ||
var serverUrl = UrlUtil.getBaseUrl(); | ||
var extension = getExtension(searchHit); | ||
if(extension == null) { | ||
return null; | ||
} | ||
|
||
var latest = versions.getLatest(extension, null, false, true); | ||
var searchEntry = latest.toSearchEntryJson(); | ||
searchEntry.url = createApiUrl(serverUrl, "api", searchEntry.namespace, searchEntry.name); | ||
searchEntry.files = storageUtil.getFileUrls(latest, serverUrl, DOWNLOAD, ICON); | ||
|
||
if (includeAllVersions) { | ||
var activeVersions = repositories.findActiveVersions(extension).toList(); | ||
var versionUrls = storageUtil.getFileUrls(activeVersions, serverUrl, DOWNLOAD); | ||
searchEntry.allVersions = getAllVersionReferences(activeVersions, versionUrls, serverUrl); | ||
} | ||
|
||
return searchEntry; | ||
} | ||
|
||
private Extension getExtension(SearchHit<ExtensionSearch> searchHit) { | ||
var searchItem = searchHit.getContent(); | ||
var extension = entityManager.find(Extension.class, searchItem.id); | ||
if (extension == null || !extension.isActive()) { | ||
extension = new Extension(); | ||
extension.setId(searchItem.id); | ||
search.removeSearchEntry(extension); | ||
return null; | ||
} | ||
|
||
return extension; | ||
} | ||
|
||
private List<SearchEntryJson.VersionReference> getAllVersionReferences( | ||
List<ExtensionVersion> extVersions, | ||
Map<Long, Map<String, String>> versionUrls, | ||
String serverUrl | ||
) { | ||
return extVersions.stream() | ||
.sorted(ExtensionVersion.SORT_COMPARATOR) | ||
.map(extVersion -> { | ||
var ref = new SearchEntryJson.VersionReference(); | ||
ref.version = extVersion.getVersion(); | ||
ref.engines = extVersion.getEnginesMap(); | ||
ref.url = UrlUtil.createApiVersionUrl(serverUrl, extVersion); | ||
ref.files = versionUrls.get(extVersion.getId()); | ||
return ref; | ||
}).collect(Collectors.toList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.