Search in sources :

Example 11 with Request

use of javax.ws.rs.core.Request in project com-liferay-apio-architect by liferay.

the class ErrorUtil method getErrorResponse.

/**
 * Transforms an exception into a {@code Response}.
 *
 * @param  exception the exception
 * @param  request the current request
 * @param  httpHeaders the current HTTP headers
 * @return the response
 */
public Response getErrorResponse(Exception exception, Request request, HttpHeaders httpHeaders) {
    Optional<APIError> apiErrorOptional = _exceptionConverterManager.convert(exception);
    if (!apiErrorOptional.isPresent()) {
        Class<? extends Exception> exceptionClass = exception.getClass();
        if (_apioLogger != null) {
            _apioLogger.warning("No exception converter found for " + exceptionClass);
        }
        if (exceptionClass.isAssignableFrom(WebApplicationException.class)) {
            WebApplicationException webApplicationException = (WebApplicationException) exception;
            return webApplicationException.getResponse();
        }
        return Response.serverError().build();
    }
    APIError apiError = apiErrorOptional.get();
    if (_apioLogger != null) {
        _apioLogger.error(apiError);
    }
    int statusCode = apiError.getStatusCode();
    Optional<ErrorMessageMapper> errorMessageMapperOptional = _errorMessageMapperManager.getErrorMessageMapperOptional(request);
    return errorMessageMapperOptional.map(errorMessageMapper -> {
        String result = ErrorWriter.writeError(errorMessageMapper, apiError, httpHeaders);
        return Response.status(statusCode).type(errorMessageMapper.getMediaType()).entity(result).build();
    }).orElseGet(() -> Response.status(statusCode).build());
}
Also used : ErrorWriter(com.liferay.apio.architect.writer.ErrorWriter) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) ApioLogger(com.liferay.apio.architect.logger.ApioLogger) ErrorMessageMapperManager(com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager) Component(org.osgi.service.component.annotations.Component) APIError(com.liferay.apio.architect.error.APIError) HttpHeaders(javax.ws.rs.core.HttpHeaders) Response(javax.ws.rs.core.Response) OPTIONAL(org.osgi.service.component.annotations.ReferenceCardinality.OPTIONAL) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) GREEDY(org.osgi.service.component.annotations.ReferencePolicyOption.GREEDY) Request(javax.ws.rs.core.Request) Reference(org.osgi.service.component.annotations.Reference) ExceptionConverterManager(com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager) WebApplicationException(javax.ws.rs.WebApplicationException) ErrorMessageMapper(com.liferay.apio.architect.message.json.ErrorMessageMapper) APIError(com.liferay.apio.architect.error.APIError)

Example 12 with Request

use of javax.ws.rs.core.Request in project ovirt-engine by oVirt.

the class UsageFinderTest method testAction.

@Test
public void testAction() {
    try {
        UriInfo uriInfo = mockUri("vms", "00000001-0001-0001-0001-000000000011", "freezefilesystems");
        Request request = mockRequest("POST");
        Fault fault = usageFinder.getUsageMessage(uriInfo, request);
        assertEquals("For correct usage, see: http://localhost:8080/ovirt-engine/apidoc#services/vm/methods/freeze_filesystems", fault.getDetail());
    } catch (URISyntaxException | ClassNotFoundException | IOException e) {
        fail();
    }
}
Also used : Request(javax.ws.rs.core.Request) Fault(org.ovirt.engine.api.model.Fault) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) UriInfo(javax.ws.rs.core.UriInfo) Test(org.junit.Test)

Example 13 with Request

use of javax.ws.rs.core.Request in project ovirt-engine by oVirt.

the class UsageFinderTest method mockRequest.

private Request mockRequest(String httpMethod) {
    Request requestMock = mock(Request.class);
    when(requestMock.getMethod()).thenReturn(httpMethod);
    return requestMock;
}
Also used : Request(javax.ws.rs.core.Request)

Example 14 with Request

use of javax.ws.rs.core.Request in project dropwizard by dropwizard.

the class JerseyViolationExceptionTest method testAccessors.

@Test
void testAccessors() {
    final Set<? extends ConstraintViolation<?>> violations = Collections.emptySet();
    @SuppressWarnings("unchecked") final Inflector<Request, ?> inf = mock(Inflector.class);
    final Invocable inv = Invocable.create(inf);
    final JerseyViolationException test = new JerseyViolationException(violations, inv);
    assertSame(inv, test.getInvocable());
}
Also used : Invocable(org.glassfish.jersey.server.model.Invocable) Request(javax.ws.rs.core.Request) Test(org.junit.jupiter.api.Test)

Example 15 with Request

use of javax.ws.rs.core.Request in project muikku by otavanopisto.

the class WorkspaceRESTService method getWorkspaceFileContent.

@GET
@Path("/workspaces/{WORKSPACEID}/workspacefile/{FILEIDENTIFIER}")
@RESTPermit(handling = Handling.INLINE)
public Response getWorkspaceFileContent(@PathParam("WORKSPACEID") Long workspaceId, @PathParam("FILEIDENTIFIER") String fileIdentifier, @Context Request request) {
    WorkspaceEntity workspaceEntity = workspaceEntityController.findWorkspaceEntityById(workspaceId);
    if (workspaceEntity == null)
        return Response.status(Status.BAD_REQUEST).build();
    WorkspaceEntityFile imageFile = workspaceEntityFileController.findWorkspaceEntityFile(workspaceEntity, fileIdentifier);
    if (imageFile == null)
        return Response.status(Status.NOT_FOUND).build();
    StreamingOutput output = s -> fileController.outputFileToStream("workspace", imageFile.getDiskName(), s);
    String contentType = imageFile.getContentType();
    String tagIdentifier = String.format("%d-%s-%d", imageFile.getWorkspaceEntity(), imageFile.getDiskName(), imageFile.getLastModified().getTime());
    EntityTag tag = new EntityTag(DigestUtils.md5Hex(String.valueOf(tagIdentifier)));
    ResponseBuilder builder = request.evaluatePreconditions(tag);
    if (builder != null) {
        return builder.build();
    }
    CacheControl cacheControl = new CacheControl();
    cacheControl.setMustRevalidate(true);
    return Response.ok().cacheControl(cacheControl).tag(tag).type(contentType).entity(output).build();
}
Also used : WorkspaceEntityFileRESTModel(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceEntityFileRESTModel) Arrays(java.util.Arrays) Produces(javax.ws.rs.Produces) WorkspaceCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceCompositeReply) EnvironmentRoleArchetype(fi.otavanopisto.muikku.model.users.EnvironmentRoleArchetype) WorkspaceMaterialFileFieldAnswerFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialFileFieldAnswerFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) Material(fi.otavanopisto.muikku.plugins.material.model.Material) Flag(fi.otavanopisto.muikku.model.users.Flag) StringUtils(org.apache.commons.lang3.StringUtils) User(fi.otavanopisto.muikku.schooldata.entity.User) WorkspaceEntityFileController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceEntityFileController) WorkspaceMaterialAudioFieldAnswerClip(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAudioFieldAnswerClip) Map(java.util.Map) CourseMetaController(fi.otavanopisto.muikku.schooldata.CourseMetaController) WorkspaceMaterialCompositeReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialCompositeReply) Instance(javax.enterprise.inject.Instance) WorkspaceDetails(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceDetails) WorkspaceFeeInfo(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceFeeInfo) WorkspaceFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceFolder) WorkspaceEntityController(fi.otavanopisto.muikku.schooldata.WorkspaceEntityController) AssessmentRequestController(fi.otavanopisto.muikku.plugins.assessmentrequest.AssessmentRequestController) SchoolDataIdentifier(fi.otavanopisto.muikku.schooldata.SchoolDataIdentifier) HtmlMaterial(fi.otavanopisto.muikku.plugins.material.model.HtmlMaterial) Set(java.util.Set) TempFileUtils(fi.otavanopisto.muikku.files.TempFileUtils) IOUtils(org.apache.commons.io.IOUtils) WorkspaceMaterialProducer(fi.otavanopisto.muikku.model.workspace.WorkspaceMaterialProducer) WorkspaceType(fi.otavanopisto.muikku.schooldata.entity.WorkspaceType) WorkspaceFieldIOException(fi.otavanopisto.muikku.plugins.workspace.fieldio.WorkspaceFieldIOException) DatatypeConverter(javax.xml.bind.DatatypeConverter) WorkspaceIndexer(fi.otavanopisto.muikku.plugins.search.WorkspaceIndexer) ZipOutputStream(java.util.zip.ZipOutputStream) GET(javax.ws.rs.GET) MuikkuPermissions(fi.otavanopisto.muikku.security.MuikkuPermissions) WorkspaceMaterialField(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialField) RESTPermitUnimplemented(fi.otavanopisto.muikku.rest.RESTPermitUnimplemented) Handling(fi.otavanopisto.security.rest.RESTPermit.Handling) WorkspaceVisitController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceVisitController) ArrayList(java.util.ArrayList) SessionController(fi.otavanopisto.muikku.session.SessionController) WorkspaceJournalEntryRESTModel(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceJournalEntryRESTModel) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) WorkspaceMaterialReplyController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialReplyController) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) WorkspaceMaterialFieldAnswer(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialFieldAnswer) IOException(java.io.IOException) MaterialViewRestrict(fi.otavanopisto.muikku.plugins.material.model.MaterialViewRestrict) WorkspaceMaterial(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterial) Stateful(javax.ejb.Stateful) RestCatchSchoolDataExceptions(fi.otavanopisto.muikku.schooldata.RestCatchSchoolDataExceptions) WorkspaceUserEntityController(fi.otavanopisto.muikku.users.WorkspaceUserEntityController) SearchProvider(fi.otavanopisto.muikku.search.SearchProvider) WorkspaceMaterialFieldController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialFieldController) WorkspaceNodeType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNodeType) MaterialController(fi.otavanopisto.muikku.plugins.material.MaterialController) WorkspaceMaterialController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialController) WorkspaceMaterialReplyState(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialReplyState) Date(java.util.Date) Path(javax.ws.rs.Path) BooleanPredicate(fi.otavanopisto.muikku.model.base.BooleanPredicate) QueryParam(javax.ws.rs.QueryParam) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultValue(javax.ws.rs.DefaultValue) Any(javax.enterprise.inject.Any) Workspace(fi.otavanopisto.muikku.schooldata.entity.Workspace) ZipEntry(java.util.zip.ZipEntry) Sort(fi.otavanopisto.muikku.search.SearchProvider.Sort) DELETE(javax.ws.rs.DELETE) Context(javax.ws.rs.core.Context) WorkspaceJournalEntry(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceJournalEntry) WorkspaceMaterialAssignmentType(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceMaterialAssignmentType) Collection(java.util.Collection) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceMaterialContainsAnswersExeption(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialContainsAnswersExeption) StreamingOutput(javax.ws.rs.core.StreamingOutput) Logger(java.util.logging.Logger) GradingController(fi.otavanopisto.muikku.schooldata.GradingController) Objects(java.util.Objects) EducationType(fi.otavanopisto.muikku.schooldata.entity.EducationType) List(java.util.List) OffsetDateTime(java.time.OffsetDateTime) UserEntityController(fi.otavanopisto.muikku.users.UserEntityController) Response(javax.ws.rs.core.Response) UserController(fi.otavanopisto.muikku.users.UserController) WorkspaceNode(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceNode) WorkspaceMaterialReply(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceMaterialReply) WorkspaceRoleArchetype(fi.otavanopisto.muikku.model.workspace.WorkspaceRoleArchetype) Request(javax.ws.rs.core.Request) CacheControl(javax.ws.rs.core.CacheControl) WorkspaceStudent(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStudent) UserEntity(fi.otavanopisto.muikku.model.users.UserEntity) UserIndexer(fi.otavanopisto.muikku.plugins.search.UserIndexer) WorkspaceController(fi.otavanopisto.muikku.schooldata.WorkspaceController) PathParam(javax.ws.rs.PathParam) WorkspaceRootFolder(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceRootFolder) FlagController(fi.otavanopisto.muikku.users.FlagController) WorkspaceUser(fi.otavanopisto.muikku.schooldata.entity.WorkspaceUser) HashMap(java.util.HashMap) WorkspaceMaterialDeleteError(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialDeleteError) Level(java.util.logging.Level) MessagingWidget(fi.otavanopisto.muikku.controller.messaging.MessagingWidget) HashSet(java.util.HashSet) Inject(javax.inject.Inject) FileController(fi.otavanopisto.muikku.plugins.data.FileController) CollectionUtils(org.apache.commons.collections.CollectionUtils) Status(javax.ws.rs.core.Response.Status) OutputStream(java.io.OutputStream) POST(javax.ws.rs.POST) Iterator(java.util.Iterator) WorkspaceUserEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceUserEntity) WorkspaceStaffMember(fi.otavanopisto.muikku.plugins.workspace.rest.model.WorkspaceStaffMember) SearchResult(fi.otavanopisto.muikku.search.SearchResult) EntityTag(javax.ws.rs.core.EntityTag) WorkspaceJournalController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceJournalController) PluginRESTService(fi.otavanopisto.muikku.plugin.PluginRESTService) RequestScoped(javax.enterprise.context.RequestScoped) UserSchoolDataIdentifier(fi.otavanopisto.muikku.model.users.UserSchoolDataIdentifier) QueryFieldController(fi.otavanopisto.muikku.plugins.material.QueryFieldController) WorkspaceMaterialFieldAnswerController(fi.otavanopisto.muikku.plugins.workspace.WorkspaceMaterialFieldAnswerController) SchoolDataBridgeSessionController(fi.otavanopisto.muikku.schooldata.SchoolDataBridgeSessionController) PUT(javax.ws.rs.PUT) Comparator(java.util.Comparator) DigestUtils(org.apache.commons.codec.digest.DigestUtils) Collections(java.util.Collections) InputStream(java.io.InputStream) WorkspaceEntityFile(fi.otavanopisto.muikku.plugins.workspace.model.WorkspaceEntityFile) WorkspaceEntity(fi.otavanopisto.muikku.model.workspace.WorkspaceEntity) StreamingOutput(javax.ws.rs.core.StreamingOutput) EntityTag(javax.ws.rs.core.EntityTag) CacheControl(javax.ws.rs.core.CacheControl) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Path(javax.ws.rs.Path) RESTPermit(fi.otavanopisto.security.rest.RESTPermit) GET(javax.ws.rs.GET)

Aggregations

Request (javax.ws.rs.core.Request)19 Response (javax.ws.rs.core.Response)12 Test (org.junit.Test)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)9 IOException (java.io.IOException)4 UriInfo (javax.ws.rs.core.UriInfo)3 URISyntaxException (java.net.URISyntaxException)2 List (java.util.List)2 EntityTag (javax.ws.rs.core.EntityTag)2 ApplicationContext (org.everrest.core.ApplicationContext)2 GenericContainerResponse (org.everrest.core.GenericContainerResponse)2 Fault (org.ovirt.engine.api.model.Fault)2 HashCode (com.google.common.hash.HashCode)1 HashFunction (com.google.common.hash.HashFunction)1 Hasher (com.google.common.hash.Hasher)1 APIError (com.liferay.apio.architect.error.APIError)1 ApioLogger (com.liferay.apio.architect.logger.ApioLogger)1 ErrorMessageMapper (com.liferay.apio.architect.message.json.ErrorMessageMapper)1 ExceptionConverterManager (com.liferay.apio.architect.wiring.osgi.manager.ExceptionConverterManager)1 ErrorMessageMapperManager (com.liferay.apio.architect.wiring.osgi.manager.message.json.ErrorMessageMapperManager)1