Skip to content

Commit

Permalink
Merge pull request #906 from amvanbaren/constructor-injection
Browse files Browse the repository at this point in the history
Use constructor injection
  • Loading branch information
amvanbaren committed Apr 23, 2024
2 parents e5433c0 + 9b554ae commit 876f0df
Show file tree
Hide file tree
Showing 84 changed files with 1,338 additions and 912 deletions.
43 changes: 23 additions & 20 deletions server/src/main/java/org/eclipse/openvsx/ExtensionService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,8 @@
********************************************************************************/
package org.eclipse.openvsx;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;

import jakarta.transaction.Transactional;
import jakarta.transaction.Transactional.TxType;

import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.cache.CacheService;
import org.eclipse.openvsx.entities.*;
Expand All @@ -28,31 +20,42 @@
import org.eclipse.openvsx.util.ErrorResultException;
import org.eclipse.openvsx.util.TempFile;
import org.eclipse.openvsx.util.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.time.LocalDateTime;
import java.util.LinkedHashSet;

@Component
public class ExtensionService {

private static final int MAX_CONTENT_SIZE = 512 * 1024 * 1024;

@Autowired
RepositoryService repositories;

@Autowired
SearchUtilService search;

@Autowired
CacheService cache;

@Autowired
PublishExtensionVersionHandler publishHandler;
private final RepositoryService repositories;
private final SearchUtilService search;
private final CacheService cache;
private final PublishExtensionVersionHandler publishHandler;

@Value("${ovsx.publishing.require-license:false}")
boolean requireLicense;

public ExtensionService(
RepositoryService repositories,
SearchUtilService search,
CacheService cache,
PublishExtensionVersionHandler publishHandler
) {
this.repositories = repositories;
this.search = search;
this.cache = cache;
this.publishHandler = publishHandler;
}

@Transactional
public ExtensionVersion mirrorVersion(TempFile extensionFile, String signatureName, PersonalAccessToken token, String binaryName, String timestamp) {
var download = doPublish(extensionFile, binaryName, token, TimeUtil.fromUTCString(timestamp), false);
Expand Down
70 changes: 37 additions & 33 deletions server/src/main/java/org/eclipse/openvsx/LocalRegistryService.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.eclipse.openvsx.util.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.PageRequest;
Expand All @@ -52,38 +51,43 @@ public class LocalRegistryService implements IExtensionRegistry {

protected final Logger logger = LoggerFactory.getLogger(LocalRegistryService.class);

@Autowired
EntityManager entityManager;

@Autowired
RepositoryService repositories;

@Autowired
ExtensionService extensions;

@Autowired
VersionService versions;

@Autowired
UserService users;

@Autowired
SearchUtilService search;

@Autowired
ExtensionValidator validator;

@Autowired
StorageUtilService storageUtil;

@Autowired
EclipseService eclipse;

@Autowired
CacheService cache;

@Autowired
ExtensionVersionIntegrityService integrityService;
private final EntityManager entityManager;
private final RepositoryService repositories;
private final ExtensionService extensions;
private final VersionService versions;
private final UserService users;
private final SearchUtilService search;
private final ExtensionValidator validator;
private final StorageUtilService storageUtil;
private final EclipseService eclipse;
private final CacheService cache;
private final ExtensionVersionIntegrityService integrityService;

public LocalRegistryService(
EntityManager entityManager,
RepositoryService repositories,
ExtensionService extensions,
VersionService versions,
UserService users,
SearchUtilService search,
ExtensionValidator validator,
StorageUtilService storageUtil,
EclipseService eclipse,
CacheService cache,
ExtensionVersionIntegrityService integrityService
) {
this.entityManager = entityManager;
this.repositories = repositories;
this.extensions = extensions;
this.versions = versions;
this.users = users;
this.search = search;
this.validator = validator;
this.storageUtil = storageUtil;
this.eclipse = eclipse;
this.cache = cache;
this.integrityService = integrityService;
}

@Value("${ovsx.registry.version:}")
String registryVersion;
Expand Down
22 changes: 13 additions & 9 deletions server/src/main/java/org/eclipse/openvsx/RegistryAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.openvsx.entities.SemanticVersion;
import org.eclipse.openvsx.json.*;
import org.eclipse.openvsx.search.ISearchService;
import org.eclipse.openvsx.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ResponseStatusException;

import jakarta.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.net.URI;
import java.util.ArrayList;
Expand All @@ -47,14 +46,19 @@ public class RegistryAPI {
private final static int REVIEW_COMMENT_SIZE = 2048;
private final static String VERSION_PATH_PARAM_REGEX = "(?:" + SemanticVersion.VERSION_PATH_PARAM_REGEX + ")|latest|pre-release";

@Autowired
LocalRegistryService local;

@Autowired
UpstreamRegistryService upstream;
private final LocalRegistryService local;
private final UpstreamRegistryService upstream;
private final UserService users;

@Autowired
UserService users;
public RegistryAPI(
LocalRegistryService local,
UpstreamRegistryService upstream,
UserService users
) {
this.local = local;
this.upstream = upstream;
this.users = users;
}

protected Iterable<IExtensionRegistry> getRegistries() {
var registries = new ArrayList<IExtensionRegistry>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,41 @@
import org.eclipse.openvsx.search.ISearchService;
import org.eclipse.openvsx.util.NotFoundException;
import org.eclipse.openvsx.util.TargetPlatform;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.HttpStatusCodeException;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.util.UriComponentsBuilder;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Component
public class UpstreamRegistryService implements IExtensionRegistry {

protected final Logger logger = LoggerFactory.getLogger(UpstreamRegistryService.class);

@Autowired
RestTemplate restTemplate;

@Autowired(required = false)
UpstreamProxyService proxy;

@Autowired
UrlConfigService urlConfigService;
private final RestTemplate restTemplate;
private UpstreamProxyService proxy;
private final UrlConfigService urlConfigService;

public UpstreamRegistryService(
RestTemplate restTemplate,
Optional<UpstreamProxyService> upstreamProxyService,
UrlConfigService urlConfigService
) {
this.restTemplate = restTemplate;
upstreamProxyService.ifPresent(service -> this.proxy = service);
this.urlConfigService = urlConfigService;
}

public boolean isValid() {
return !StringUtils.isEmpty(urlConfigService.getUpstreamUrl());
Expand Down
28 changes: 16 additions & 12 deletions server/src/main/java/org/eclipse/openvsx/UserAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.eclipse.openvsx.util.ErrorResultException;
import org.eclipse.openvsx.util.NotFoundException;
import org.eclipse.openvsx.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
Expand All @@ -45,17 +44,22 @@ public class UserAPI {

private final static int TOKEN_DESCRIPTION_SIZE = 255;

@Autowired
RepositoryService repositories;

@Autowired
UserService users;

@Autowired
EclipseService eclipse;

@Autowired
StorageUtilService storageUtil;
private final RepositoryService repositories;
private final UserService users;
private final EclipseService eclipse;
private final StorageUtilService storageUtil;

public UserAPI(
RepositoryService repositories,
UserService users,
EclipseService eclipse,
StorageUtilService storageUtil
) {
this.repositories = repositories;
this.users = users;
this.eclipse = eclipse;
this.storageUtil = storageUtil;
}

/**
* Redirect to GitHub Oauth2 login as default login provider.
Expand Down
38 changes: 21 additions & 17 deletions server/src/main/java/org/eclipse/openvsx/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
package org.eclipse.openvsx;

import com.google.common.base.Joiner;
import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import org.eclipse.openvsx.cache.CacheService;
import org.eclipse.openvsx.entities.*;
import org.eclipse.openvsx.json.AccessTokenJson;
Expand All @@ -22,14 +24,11 @@
import org.eclipse.openvsx.util.NotFoundException;
import org.eclipse.openvsx.util.TimeUtil;
import org.eclipse.openvsx.util.UrlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.stereotype.Component;

import jakarta.persistence.EntityManager;
import jakarta.transaction.Transactional;
import java.util.Objects;
import java.util.UUID;

Expand All @@ -39,20 +38,25 @@
@Component
public class UserService {

@Autowired
EntityManager entityManager;

@Autowired
RepositoryService repositories;

@Autowired
StorageUtilService storageUtil;

@Autowired
CacheService cache;

@Autowired
ExtensionValidator validator;
private final EntityManager entityManager;
private final RepositoryService repositories;
private final StorageUtilService storageUtil;
private final CacheService cache;
private final ExtensionValidator validator;

public UserService(
EntityManager entityManager,
RepositoryService repositories,
StorageUtilService storageUtil,
CacheService cache,
ExtensionValidator validator
) {
this.entityManager = entityManager;
this.repositories = repositories;
this.storageUtil = storageUtil;
this.cache = cache;
this.validator = validator;
}

public UserData findLoggedInUser() {
var authentication = SecurityContextHolder.getContext().getAuthentication();
Expand Down
Loading

0 comments on commit 876f0df

Please sign in to comment.