-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validation of devfiles on workspace update
- Loading branch information
1 parent
cf76ba8
commit a969f69
Showing
12 changed files
with
623 additions
and
86 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
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
126 changes: 126 additions & 0 deletions
126
...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,126 @@ | ||
/* | ||
* 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 com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
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.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(); | ||
|
||
@Inject | ||
public WorkspaceEntityProvider(DevfileManager devfileManager) { | ||
this.devfileManager = devfileManager; | ||
} | ||
|
||
@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 { | ||
try { | ||
JsonNode wsNode = mapper.readTree(entityStream); | ||
JsonNode devfileNode = wsNode.path("devfile"); | ||
if (!devfileNode.isNull()) { | ||
devfileManager.parseJson(devfileNode.toString()); | ||
} | ||
return DtoFactory.getInstance().createDtoFromJson(wsNode.toString(), WorkspaceDto.class); | ||
} catch (DevfileFormatException e) { | ||
throw new BadRequestException(e.getMessage()); | ||
} catch (IOException e) { | ||
throw new WebApplicationException(e.getMessage(), e); | ||
} | ||
} | ||
|
||
@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(); | ||
} | ||
} | ||
} |
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.