DEFAULT_DEVFILE = Map.of("schemaVersion", "2.3.0");
+
private Constants() {}
}
diff --git a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryDto.java b/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryDto.java
deleted file mode 100644
index 8d845467899..00000000000
--- a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryDto.java
+++ /dev/null
@@ -1,78 +0,0 @@
-/*
- * Copyright (c) 2012-2021 Red Hat, Inc.
- * This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License 2.0
- * which is available at https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * Red Hat, Inc. - initial API and implementation
- */
-package org.eclipse.che.api.factory.shared.dto;
-
-import static org.eclipse.che.api.core.factory.FactoryParameter.Obligation.OPTIONAL;
-
-import java.util.List;
-import org.eclipse.che.api.core.factory.FactoryParameter;
-import org.eclipse.che.api.core.model.factory.Factory;
-import org.eclipse.che.api.core.rest.shared.dto.Hyperlinks;
-import org.eclipse.che.api.core.rest.shared.dto.Link;
-import org.eclipse.che.api.workspace.shared.dto.WorkspaceConfigDto;
-import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto;
-import org.eclipse.che.dto.shared.DTO;
-
-/**
- * Factory of version 4.0.
- *
- * This 'implementation' of {@link FactoryMetaDto} is used for Devfile v1.
- *
- * @author Max Shaposhnik
- */
-@DTO
-public interface FactoryDto extends FactoryMetaDto, Factory, Hyperlinks {
-
- @Override
- default FactoryMetaDto acceptVisitor(FactoryVisitor visitor) {
- return visitor.visit(this);
- }
-
- @FactoryParameter(obligation = OPTIONAL)
- DevfileDto getDevfile();
-
- void setDevfile(DevfileDto devfileDto);
-
- FactoryDto withDevfile(DevfileDto devfileDto);
-
- FactoryDto withV(String v);
-
- @Override
- FactoryDto withName(String name);
-
- @Override
- FactoryDto withPolicies(PoliciesDto policies);
-
- @Override
- FactoryDto withIde(IdeDto ide);
-
- @Override
- FactoryDto withId(String id);
-
- @Override
- FactoryDto withSource(String source);
-
- @Override
- FactoryDto withCreator(AuthorDto creator);
-
- @Override
- FactoryDto withLinks(List links);
-
- /** because factory DTO may have devfile, in that case, workspace may be optional */
- @Override
- @FactoryParameter(obligation = OPTIONAL)
- WorkspaceConfigDto getWorkspace();
-
- void setWorkspace(WorkspaceConfigDto workspace);
-
- FactoryDto withWorkspace(WorkspaceConfigDto workspace);
-}
diff --git a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryVisitor.java b/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryVisitor.java
index 3d9eead20c4..e7d9d005706 100644
--- a/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryVisitor.java
+++ b/wsmaster/che-core-api-factory-shared/src/main/java/org/eclipse/che/api/factory/shared/dto/FactoryVisitor.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012-2023 Red Hat, Inc.
+ * Copyright (c) 2012-2024 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -17,17 +17,6 @@
*/
public interface FactoryVisitor {
- /**
- * Visit factory with devfile v1.
- *
- *
Implementation should update given factory with needed changes and give it back.
- *
- * @param factoryDto factory to visit
- * @return updated factory
- */
- @Deprecated
- FactoryDto visit(FactoryDto factoryDto);
-
/**
* Visit factory with devfile v2.
*
diff --git a/wsmaster/che-core-api-factory/pom.xml b/wsmaster/che-core-api-factory/pom.xml
index f5db2c62092..1bcf0b49a10 100644
--- a/wsmaster/che-core-api-factory/pom.xml
+++ b/wsmaster/che-core-api-factory/pom.xml
@@ -17,7 +17,7 @@
che-master-parent
org.eclipse.che.core
- 7.88.0-SNAPSHOT
+ 7.96.0-SNAPSHOT
che-core-api-factory
jar
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/ApiExceptionMapper.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/ApiExceptionMapper.java
index 122c5b4d34f..f119ea40230 100644
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/ApiExceptionMapper.java
+++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/ApiExceptionMapper.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012-2023 Red Hat, Inc.
+ * Copyright (c) 2012-2024 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -11,11 +11,16 @@
*/
package org.eclipse.che.api.factory.server;
+import static com.google.common.base.Strings.isNullOrEmpty;
+import static org.eclipse.che.dto.server.DtoFactory.newDto;
+
+import java.util.Collections;
import java.util.Map;
import org.eclipse.che.api.core.ApiException;
import org.eclipse.che.api.core.BadRequestException;
import org.eclipse.che.api.core.ServerException;
import org.eclipse.che.api.core.UnauthorizedException;
+import org.eclipse.che.api.core.rest.shared.dto.ExtendedError;
import org.eclipse.che.api.factory.server.scm.exception.ScmCommunicationException;
import org.eclipse.che.api.factory.server.scm.exception.ScmUnauthorizedException;
import org.eclipse.che.api.factory.server.scm.exception.UnknownScmProviderException;
@@ -48,6 +53,16 @@ public static ApiException toApiException(ScmUnauthorizedException scmUnauthoriz
+ scmUnauthorizedException.getMessage());
}
+ public static ApiException toApiException(ScmCommunicationException scmCommunicationException) {
+ ApiException apiException = getApiException(scmCommunicationException);
+ return (apiException != null)
+ ? apiException
+ : new ServerException(
+ "Error occurred during SCM communication."
+ + "Cause: "
+ + scmCommunicationException.getMessage());
+ }
+
public static ApiException toApiException(
DevfileException devfileException, DevfileLocation location) {
ApiException cause = getApiException(devfileException.getCause());
@@ -70,15 +85,28 @@ private static ApiException getApiException(Throwable throwable) {
"oauth_version", scmCause.getOauthVersion(),
"oauth_provider", scmCause.getOauthProvider(),
"oauth_authentication_url", scmCause.getAuthenticateUrl()));
- } else if (throwable instanceof UnknownScmProviderException) {
- return new ServerException(
- "Provided location is unknown or misconfigured on the server side. Error message: "
- + throwable.getMessage());
- } else if (throwable instanceof ScmCommunicationException) {
- return new ServerException(
- "There is an error happened when communicate with SCM server. Error message: "
- + throwable.getMessage());
+ } else {
+ if (throwable instanceof UnknownScmProviderException) {
+ return new ServerException(
+ appendErrorMessage(
+ "Provided location is unknown or misconfigured on the server side",
+ throwable.getMessage()));
+ } else if (throwable instanceof ScmCommunicationException) {
+ return new ServerException(
+ newDto(ExtendedError.class)
+ .withMessage(
+ appendErrorMessage(
+ "Error occurred during SCM communication.", throwable.getMessage()))
+ .withErrorCode(404)
+ .withAttributes(
+ Collections.singletonMap(
+ "provider", ((ScmCommunicationException) throwable).getProvider())));
+ }
}
return null;
}
+
+ private static String appendErrorMessage(String message, String errorMessage) {
+ return message + (isNullOrEmpty(errorMessage) ? "" : " Error message: " + errorMessage);
+ }
}
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java
index b5b80980449..08efe28e43e 100644
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java
+++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/BaseFactoryParameterResolver.java
@@ -13,6 +13,7 @@
import static java.util.stream.Collectors.toMap;
import static org.eclipse.che.api.factory.shared.Constants.CURRENT_VERSION;
+import static org.eclipse.che.api.factory.shared.Constants.DEFAULT_DEVFILE;
import static org.eclipse.che.dto.server.DtoFactory.newDto;
import java.util.Collections;
@@ -24,7 +25,7 @@
import org.eclipse.che.api.factory.server.scm.AuthorisationRequestManager;
import org.eclipse.che.api.factory.server.urlfactory.RemoteFactoryUrl;
import org.eclipse.che.api.factory.server.urlfactory.URLFactoryBuilder;
-import org.eclipse.che.api.factory.shared.dto.FactoryDto;
+import org.eclipse.che.api.factory.shared.dto.FactoryDevfileV2Dto;
import org.eclipse.che.api.factory.shared.dto.FactoryMetaDto;
import org.eclipse.che.api.factory.shared.dto.FactoryVisitor;
import org.eclipse.che.api.workspace.server.devfile.FileContentProvider;
@@ -60,7 +61,12 @@ protected FactoryMetaDto createFactory(
contentProvider,
extractOverrideParams(factoryParameters),
getSkipAuthorisation(factoryParameters))
- .orElseGet(() -> newDto(FactoryDto.class).withV(CURRENT_VERSION).withSource("repo"))
+ .orElseGet(
+ () ->
+ newDto(FactoryDevfileV2Dto.class)
+ .withDevfile(DEFAULT_DEVFILE)
+ .withV(CURRENT_VERSION)
+ .withSource("repo"))
.acceptVisitor(factoryVisitor);
}
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/DtoConverter.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/DtoConverter.java
index cf9b5972260..526ba6c18e7 100644
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/DtoConverter.java
+++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/DtoConverter.java
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2012-2021 Red Hat, Inc.
+ * Copyright (c) 2012-2024 Red Hat, Inc.
* This program and the accompanying materials are made
* available under the terms of the Eclipse Public License 2.0
* which is available at https://www.eclipse.org/legal/epl-2.0/
@@ -17,7 +17,6 @@
import java.util.List;
import org.eclipse.che.api.core.model.factory.Action;
import org.eclipse.che.api.core.model.factory.Author;
-import org.eclipse.che.api.core.model.factory.Factory;
import org.eclipse.che.api.core.model.factory.Ide;
import org.eclipse.che.api.core.model.factory.OnAppClosed;
import org.eclipse.che.api.core.model.factory.OnAppLoaded;
@@ -25,7 +24,6 @@
import org.eclipse.che.api.core.model.factory.Policies;
import org.eclipse.che.api.core.model.user.User;
import org.eclipse.che.api.factory.shared.dto.AuthorDto;
-import org.eclipse.che.api.factory.shared.dto.FactoryDto;
import org.eclipse.che.api.factory.shared.dto.IdeActionDto;
import org.eclipse.che.api.factory.shared.dto.IdeDto;
import org.eclipse.che.api.factory.shared.dto.OnAppClosedDto;
@@ -40,29 +38,6 @@
*/
public final class DtoConverter {
- public static FactoryDto asDto(Factory factory, User user) {
- final FactoryDto factoryDto =
- newDto(FactoryDto.class)
- .withId(factory.getId())
- .withName(factory.getName())
- .withV(factory.getV());
-
- if (factory.getWorkspace() != null) {
- factoryDto.withWorkspace(
- org.eclipse.che.api.workspace.server.DtoConverter.asDto(factory.getWorkspace()));
- }
- if (factory.getCreator() != null) {
- factoryDto.withCreator(asDto(factory.getCreator(), user));
- }
- if (factory.getIde() != null) {
- factoryDto.withIde(asDto(factory.getIde()));
- }
- if (factory.getPolicies() != null) {
- factoryDto.withPolicies(asDto(factory.getPolicies()));
- }
- return factoryDto;
- }
-
public static IdeDto asDto(Ide ide) {
final IdeDto ideDto = newDto(IdeDto.class);
final OnAppClosed onAppClosed = ide.getOnAppClosed();
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryCreateValidator.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryCreateValidator.java
deleted file mode 100644
index 63b191fc382..00000000000
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryCreateValidator.java
+++ /dev/null
@@ -1,38 +0,0 @@
-/*
- * Copyright (c) 2012-2018 Red Hat, Inc.
- * This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License 2.0
- * which is available at https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * Red Hat, Inc. - initial API and implementation
- */
-package org.eclipse.che.api.factory.server;
-
-import org.eclipse.che.api.core.BadRequestException;
-import org.eclipse.che.api.core.ForbiddenException;
-import org.eclipse.che.api.core.NotFoundException;
-import org.eclipse.che.api.core.ServerException;
-import org.eclipse.che.api.factory.shared.dto.FactoryDto;
-
-/**
- * Interface for validations of factory creation stage.
- *
- * @author Alexander Garagatyi
- */
-public interface FactoryCreateValidator {
-
- /**
- * Validates factory object on creation stage. Implementation should throw exception if factory
- * object is invalid.
- *
- * @param factory factory object to validate
- * @throws BadRequestException in case if factory is not valid
- * @throws ServerException when any server error occurs
- * @throws ForbiddenException when user have no access rights for factory creation
- */
- void validateOnCreate(FactoryDto factory)
- throws BadRequestException, ServerException, ForbiddenException, NotFoundException;
-}
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryEditValidator.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryEditValidator.java
deleted file mode 100644
index e99da51e50c..00000000000
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryEditValidator.java
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * Copyright (c) 2012-2018 Red Hat, Inc.
- * This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License 2.0
- * which is available at https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * Red Hat, Inc. - initial API and implementation
- */
-package org.eclipse.che.api.factory.server;
-
-import org.eclipse.che.api.core.ForbiddenException;
-import org.eclipse.che.api.core.ServerException;
-import org.eclipse.che.api.core.model.factory.Factory;
-
-/**
- * This validator ensures that a factory can be edited by a user that has the associated rights
- * (author or account owner)
- *
- * @author Florent Benoit
- */
-public interface FactoryEditValidator {
-
- /**
- * Validates given factory by checking the current user is granted to edit the factory.
- *
- * @param factory factory object to validate
- * @throws ForbiddenException when the current user is not granted to edit the factory
- * @throws ServerException when any other error occurs
- */
- void validate(Factory factory) throws ForbiddenException, ServerException;
-}
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryService.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryService.java
index 2070ff1117b..86aef36214f 100644
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryService.java
+++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/FactoryService.java
@@ -152,17 +152,21 @@ public void refreshToken(@Parameter(description = "Factory url") @QueryParam("ur
factoryParametersResolverHolder.getFactoryParametersResolver(
singletonMap(URL_PARAMETER_NAME, url));
if (!authorisationRequestManager.isStored(factoryParametersResolver.getProviderName())) {
- personalAccessTokenManager.getAndStore(
- // get the provider URL from the factory URL
- factoryParametersResolver.parseFactoryUrl(url).getProviderUrl());
+ String scmServerUrl = factoryParametersResolver.parseFactoryUrl(url).getProviderUrl();
+ if (Boolean.parseBoolean(System.getenv("CHE_FORCE_REFRESH_PERSONAL_ACCESS_TOKEN"))) {
+ personalAccessTokenManager.forceRefreshPersonalAccessToken(scmServerUrl);
+ } else {
+ personalAccessTokenManager.getAndStore(scmServerUrl);
+ }
}
- } catch (ScmCommunicationException
- | ScmConfigurationPersistenceException
- | UnknownScmProviderException
- | UnsatisfiedScmPreconditionException e) {
+ } catch (ScmConfigurationPersistenceException | UnsatisfiedScmPreconditionException e) {
throw new ApiException(e);
} catch (ScmUnauthorizedException e) {
throw toApiException(e);
+ } catch (ScmCommunicationException e) {
+ throw toApiException(e);
+ } catch (UnknownScmProviderException e) {
+ // ignore the exception as it is not a problem if the provider from the given URL is unknown
}
}
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java
index 1896fc699b5..c26e81c490b 100644
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java
+++ b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/RawDevfileUrlFactoryParameterResolver.java
@@ -45,7 +45,8 @@ public class RawDevfileUrlFactoryParameterResolver extends BaseFactoryParameterR
implements FactoryParametersResolver {
private static final String PROVIDER_NAME = "raw-devfile-url";
- private static final Pattern PATTERN = Pattern.compile("^https?://.*\\.ya?ml(\\?token=.*)?$");
+ private static final Pattern PATTERN =
+ Pattern.compile("^https?://.*\\.ya?ml((\\?token=.*)|(\\?at=refs/heads/.*))?$");
protected final URLFactoryBuilder urlFactoryBuilder;
protected final URLFetcher urlFetcher;
diff --git a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/builder/FactoryBuilder.java b/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/builder/FactoryBuilder.java
deleted file mode 100644
index 526abd6780a..00000000000
--- a/wsmaster/che-core-api-factory/src/main/java/org/eclipse/che/api/factory/server/builder/FactoryBuilder.java
+++ /dev/null
@@ -1,314 +0,0 @@
-/*
- * Copyright (c) 2012-2018 Red Hat, Inc.
- * This program and the accompanying materials are made
- * available under the terms of the Eclipse Public License 2.0
- * which is available at https://www.eclipse.org/legal/epl-2.0/
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * Contributors:
- * Red Hat, Inc. - initial API and implementation
- */
-package org.eclipse.che.api.factory.server.builder;
-
-import static org.eclipse.che.api.core.factory.FactoryParameter.Obligation;
-import static org.eclipse.che.api.core.factory.FactoryParameter.Version;
-
-import com.google.common.base.CaseFormat;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.Reader;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.ParameterizedType;
-import java.lang.reflect.Type;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import java.util.Map;
-import javax.inject.Inject;
-import javax.inject.Singleton;
-import org.eclipse.che.api.core.ApiException;
-import org.eclipse.che.api.core.ConflictException;
-import org.eclipse.che.api.core.factory.FactoryParameter;
-import org.eclipse.che.api.core.model.workspace.config.ProjectConfig;
-import org.eclipse.che.api.core.model.workspace.config.SourceStorage;
-import org.eclipse.che.api.factory.server.FactoryConstants;
-import org.eclipse.che.api.factory.server.LegacyConverter;
-import org.eclipse.che.api.factory.server.ValueHelper;
-import org.eclipse.che.api.factory.server.impl.SourceStorageParametersValidator;
-import org.eclipse.che.api.factory.shared.dto.FactoryDto;
-import org.eclipse.che.api.workspace.shared.dto.SourceStorageDto;
-import org.eclipse.che.dto.server.DtoFactory;
-import org.eclipse.che.dto.shared.DTO;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-/**
- * Tool to easy convert Factory object to json and vise versa. Also it provides factory parameters
- * compatibility.
- *
- * @author Sergii Kabashniuk
- * @author Alexander Garagatyi
- */
-@Singleton
-public class FactoryBuilder {
- private static final Logger LOG = LoggerFactory.getLogger(FactoryBuilder.class);
-
- /** List contains all possible implementation of factory legacy converters. */
- static final List LEGACY_CONVERTERS;
-
- static {
- List l = new ArrayList<>(1);
- l.add(factory -> {});
- LEGACY_CONVERTERS = Collections.unmodifiableList(l);
- }
-
- private final SourceStorageParametersValidator sourceStorageParametersValidator;
-
- @Inject
- public FactoryBuilder(SourceStorageParametersValidator sourceStorageParametersValidator) {
- this.sourceStorageParametersValidator = sourceStorageParametersValidator;
- }
-
- /**
- * Build factory from json and validate its compatibility.
- *
- * @param json - json Reader from encoded factory.
- * @return - Factory object represented by given factory json.
- */
- public FactoryDto build(Reader json) throws IOException, ApiException {
- FactoryDto factory = DtoFactory.getInstance().createDtoFromJson(json, FactoryDto.class);
- checkValid(factory);
- return factory;
- }
-
- /**
- * Build factory from json and validate its compatibility.
- *
- * @param json - json string from encoded factory.
- * @return - Factory object represented by given factory json.
- */
- public FactoryDto build(String json) throws ApiException {
- FactoryDto factory = DtoFactory.getInstance().createDtoFromJson(json, FactoryDto.class);
- checkValid(factory);
- return factory;
- }
-
- /**
- * Build factory from json and validate its compatibility.
- *
- * @param json - json InputStream from encoded factory.
- * @return - Factory object represented by given factory json.
- */
- public FactoryDto build(InputStream json) throws IOException, ConflictException {
- FactoryDto factory = DtoFactory.getInstance().createDtoFromJson(json, FactoryDto.class);
- checkValid(factory);
- return factory;
- }
-
- /**
- * Validate factory compatibility at creation time.
- *
- * @param factory - factory object to validate
- * @throws ConflictException
- */
- public void checkValid(FactoryDto factory) throws ConflictException {
- checkValid(factory, false);
- }
-
- /**
- * Validate factory compatibility.
- *
- * @param factory - factory object to validate
- * @param isUpdate - indicates is validation performed on update time. Set-by-server variables are
- * allowed during update.
- * @throws ConflictException
- */
- public void checkValid(FactoryDto factory, boolean isUpdate) throws ConflictException {
- if (null == factory) {
- throw new ConflictException(FactoryConstants.UNPARSABLE_FACTORY_MESSAGE);
- }
- if (factory.getV() == null) {
- throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE);
- }
-
- Version v;
- try {
- v = Version.fromString(factory.getV());
- } catch (IllegalArgumentException e) {
- throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE);
- }
-
- Class usedFactoryVersionMethodProvider;
- switch (v) {
- case V4_0:
- usedFactoryVersionMethodProvider = FactoryDto.class;
- break;
- default:
- throw new ConflictException(FactoryConstants.INVALID_VERSION_MESSAGE);
- }
- validateCompatibility(
- factory, null, FactoryDto.class, usedFactoryVersionMethodProvider, v, "", isUpdate);
- }
-
- /**
- * Convert factory of given version to the latest factory format.
- *
- * @param factory - given factory.
- * @return - factory in latest format.
- * @throws org.eclipse.che.api.core.ApiException
- */
- public FactoryDto convertToLatest(FactoryDto factory) throws ApiException {
- FactoryDto resultFactory = DtoFactory.getInstance().clone(factory).withV("4.0");
- for (LegacyConverter converter : LEGACY_CONVERTERS) {
- converter.convert(resultFactory);
- }
- return resultFactory;
- }
-
- /**
- * Validate compatibility of factory parameters.
- *
- * @param object - object to validate factory parameters
- * @param parent - parent object
- * @param methodsProvider - class that provides methods with {@link
- * org.eclipse.che.api.core.factory.FactoryParameter} annotations
- * @param allowedMethodsProvider - class that provides allowed methods
- * @param version - version of factory
- * @param parentName - parent parameter queryParameterName
- * @throws org.eclipse.che.api.core.ConflictException
- */
- void validateCompatibility(
- Object object,
- Object parent,
- Class methodsProvider,
- Class allowedMethodsProvider,
- Version version,
- String parentName,
- boolean isUpdate)
- throws ConflictException {
- // validate source
- if (SourceStorageDto.class.equals(methodsProvider) && !hasSubprojectInPath(parent)) {
- sourceStorageParametersValidator.validate((SourceStorage) object, version);
- }
-
- // get all methods recursively
- for (Method method : methodsProvider.getMethods()) {
- FactoryParameter factoryParameter = method.getAnnotation(FactoryParameter.class);
- // is it factory parameter
-
- if (factoryParameter == null) {
- continue;
- }
- String fullName =
- (parentName.isEmpty() ? "" : (parentName + "."))
- + CaseFormat.UPPER_CAMEL.to(
- CaseFormat.LOWER_CAMEL,
- method.getName().startsWith("is")
- ? method.getName().substring(2)
- : method.getName().substring(3).toLowerCase());
- // check that field is set
- Object parameterValue;
- try {
- parameterValue = method.invoke(object);
- } catch (IllegalAccessException | InvocationTargetException | IllegalArgumentException e) {
- // should never happen
- LOG.error(e.getLocalizedMessage(), e);
- throw new ConflictException(FactoryConstants.INVALID_PARAMETER_MESSAGE);
- }
-
- // if value is null or empty collection or default value for primitives
- if (ValueHelper.isEmpty(parameterValue)) {
- // field must not be a mandatory, unless it's ignored or deprecated or doesn't suit to the
- // version
- if (Obligation.MANDATORY.equals(factoryParameter.obligation())
- && factoryParameter.deprecatedSince().compareTo(version) > 0
- && factoryParameter.ignoredSince().compareTo(version) > 0
- && method.getDeclaringClass().isAssignableFrom(allowedMethodsProvider)) {
- throw new ConflictException(
- String.format(FactoryConstants.MISSING_MANDATORY_MESSAGE, fullName));
- }
- } else if (!method.getDeclaringClass().isAssignableFrom(allowedMethodsProvider)) {
- throw new ConflictException(
- String.format(
- FactoryConstants.PARAMETRIZED_INVALID_PARAMETER_MESSAGE, fullName, version));
- } else {
- // is parameter deprecated
- if (factoryParameter.deprecatedSince().compareTo(version) <= 0
- || (!isUpdate && factoryParameter.setByServer())) {
- throw new ConflictException(
- String.format(
- FactoryConstants.PARAMETRIZED_INVALID_PARAMETER_MESSAGE, fullName, version));
- }
-
- // use recursion if parameter is DTO object
- if (method.getReturnType().isAnnotationPresent(DTO.class)) {
- // validate inner objects such Git ot ProjectAttributes
- validateCompatibility(
- parameterValue,
- object,
- method.getReturnType(),
- method.getReturnType(),
- version,
- fullName,
- isUpdate);
- } else if (Map.class.isAssignableFrom(method.getReturnType())) {
- Type tp = ((ParameterizedType) method.getGenericReturnType()).getActualTypeArguments()[1];
-
- Class secMapParamClass =
- (tp instanceof ParameterizedType)
- ? (Class) ((ParameterizedType) tp).getRawType()
- : (Class) tp;
- if (!String.class.equals(secMapParamClass) && !List.class.equals(secMapParamClass)) {
- if (secMapParamClass.isAnnotationPresent(DTO.class)) {
- Map