-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Devfile validation via message entity provider #14740
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
8d3c27e
Devfile validation via message body reader;
mshaposhnik 86c3e7e
Prepare for review fixes
mshaposhnik a634bdd
Prepare for review fixes
mshaposhnik 011ba36
Fix fmt
mshaposhnik 4d07aa8
Fix fmt
mshaposhnik 93e02a2
Fix fmt
mshaposhnik 0d1da70
Add some javadocs
mshaposhnik 6bb4546
Remove beta annotation
mshaposhnik 2d10ea8
Add some tests for entity readers
mshaposhnik 19d769b
fixup! Add some tests for entity readers
mshaposhnik 7c2da42
Fix review comments
mshaposhnik dd69d92
Fix deps
mshaposhnik 5b19b64
Revert header value
mshaposhnik eef0241
Merge branch 'master' into validate_2
mshaposhnik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
89 changes: 89 additions & 0 deletions
89
...e-api-core/src/main/java/org/eclipse/che/api/core/rest/WebApplicationExceptionMapper.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,89 @@ | ||
/* | ||
* 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.core.rest; | ||
|
||
import static org.eclipse.che.dto.server.DtoFactory.newDto; | ||
|
||
import javax.inject.Singleton; | ||
import javax.ws.rs.BadRequestException; | ||
import javax.ws.rs.ForbiddenException; | ||
import javax.ws.rs.NotAcceptableException; | ||
import javax.ws.rs.NotAllowedException; | ||
import javax.ws.rs.NotAuthorizedException; | ||
import javax.ws.rs.NotFoundException; | ||
import javax.ws.rs.NotSupportedException; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.Response; | ||
import javax.ws.rs.core.Response.Status; | ||
import javax.ws.rs.ext.ExceptionMapper; | ||
import javax.ws.rs.ext.Provider; | ||
import org.eclipse.che.api.core.rest.shared.dto.ServiceError; | ||
import org.eclipse.che.dto.server.DtoFactory; | ||
|
||
/** | ||
* Mapper for the {@link WebApplicationException} exceptions. | ||
* | ||
* @author Max Shaposhnyk | ||
*/ | ||
@Provider | ||
@Singleton | ||
public class WebApplicationExceptionMapper implements ExceptionMapper<WebApplicationException> { | ||
|
||
@Override | ||
public Response toResponse(WebApplicationException exception) { | ||
|
||
ServiceError error = newDto(ServiceError.class).withMessage(exception.getMessage()); | ||
|
||
if (exception instanceof BadRequestException) { | ||
return Response.status(Response.Status.BAD_REQUEST) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof ForbiddenException) { | ||
return Response.status(Response.Status.FORBIDDEN) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof NotFoundException) { | ||
return Response.status(Response.Status.NOT_FOUND) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof NotAuthorizedException) { | ||
return Response.status(Response.Status.UNAUTHORIZED) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof NotAcceptableException) { | ||
return Response.status(Status.NOT_ACCEPTABLE) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof NotAllowedException) { | ||
return Response.status(Status.METHOD_NOT_ALLOWED) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else if (exception instanceof NotSupportedException) { | ||
return Response.status(Status.UNSUPPORTED_MEDIA_TYPE) | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} else { | ||
return Response.serverError() | ||
.entity(DtoFactory.getInstance().toJson(error)) | ||
.type(MediaType.APPLICATION_JSON) | ||
.build(); | ||
} | ||
} | ||
} |
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
138 changes: 138 additions & 0 deletions
138
...workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceEntityProvider.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,138 @@ | ||
/* | ||
* 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.workspace.server; | ||
|
||
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | ||
import static org.eclipse.che.api.workspace.server.DtoConverter.asDto; | ||
|
||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.module.SimpleModule; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.io.OutputStreamWriter; | ||
import java.io.Writer; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.nio.charset.StandardCharsets; | ||
import javax.inject.Inject; | ||
import javax.inject.Singleton; | ||
import javax.ws.rs.BadRequestException; | ||
import javax.ws.rs.Consumes; | ||
import javax.ws.rs.Produces; | ||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.HttpHeaders; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyReader; | ||
import javax.ws.rs.ext.MessageBodyWriter; | ||
import javax.ws.rs.ext.Provider; | ||
import org.eclipse.che.api.workspace.server.devfile.DevfileManager; | ||
import org.eclipse.che.api.workspace.server.devfile.exception.DevfileFormatException; | ||
import org.eclipse.che.api.workspace.server.dto.DtoServerImpls.WorkspaceDtoImpl; | ||
import org.eclipse.che.api.workspace.shared.dto.WorkspaceDto; | ||
import org.eclipse.che.api.workspace.shared.dto.devfile.DevfileDto; | ||
import org.eclipse.che.dto.server.DtoFactory; | ||
|
||
/** | ||
* Entity provider for {@link WorkspaceDto}. Performs schema validation of devfile part of the | ||
* workspace before actual {@link DevfileDto} creation. | ||
* | ||
* @author Max Shaposhnyk | ||
*/ | ||
@Singleton | ||
@Provider | ||
@Produces({APPLICATION_JSON}) | ||
@Consumes({APPLICATION_JSON}) | ||
public class WorkspaceEntityProvider | ||
implements MessageBodyReader<WorkspaceDto>, MessageBodyWriter<WorkspaceDto> { | ||
|
||
private DevfileManager devfileManager; | ||
private ObjectMapper mapper = new ObjectMapper(); | ||
skabashnyuk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
@Inject | ||
public WorkspaceEntityProvider(DevfileManager devfileManager) { | ||
this.devfileManager = devfileManager; | ||
SimpleModule module = new SimpleModule(); | ||
module.addDeserializer(DevfileDto.class, new DevfileDtoDeserializer()); | ||
mapper.registerModule(module); | ||
} | ||
|
||
@Override | ||
public boolean isReadable( | ||
Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return type == WorkspaceDto.class; | ||
} | ||
|
||
@Override | ||
public WorkspaceDto readFrom( | ||
Class<WorkspaceDto> type, | ||
Type genericType, | ||
Annotation[] annotations, | ||
MediaType mediaType, | ||
MultivaluedMap<String, String> httpHeaders, | ||
InputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
return mapper | ||
.readerFor(WorkspaceDtoImpl.class) | ||
.without(DeserializationFeature.WRAP_EXCEPTIONS) | ||
.readValue(entityStream); | ||
} | ||
|
||
@Override | ||
public boolean isWriteable( | ||
Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) { | ||
return WorkspaceDto.class.isAssignableFrom(type); | ||
} | ||
|
||
@Override | ||
public long getSize( | ||
WorkspaceDto workspaceDto, | ||
Class<?> type, | ||
Type genericType, | ||
Annotation[] annotations, | ||
MediaType mediaType) { | ||
return -1; | ||
} | ||
|
||
@Override | ||
public void writeTo( | ||
WorkspaceDto workspaceDto, | ||
Class<?> type, | ||
Type genericType, | ||
Annotation[] annotations, | ||
MediaType mediaType, | ||
MultivaluedMap<String, Object> httpHeaders, | ||
OutputStream entityStream) | ||
throws IOException, WebApplicationException { | ||
httpHeaders.putSingle(HttpHeaders.CACHE_CONTROL, "public, no-cache, no-store, no-transform"); | ||
try (Writer w = new OutputStreamWriter(entityStream, StandardCharsets.UTF_8)) { | ||
w.write(DtoFactory.getInstance().toJson(workspaceDto)); | ||
w.flush(); | ||
} | ||
} | ||
|
||
class DevfileDtoDeserializer extends JsonDeserializer<DevfileDto> { | ||
@Override | ||
public DevfileDto deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { | ||
try { | ||
return asDto(devfileManager.parseJson(p.readValueAsTree().toString())); | ||
} catch (DevfileFormatException e) { | ||
throw new BadRequestException(e.getMessage()); | ||
} | ||
} | ||
} | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say we should have something like WorkspaceModule like DevfileModule. Up2you to decide.