Package org.exoplatform.services.rest

Examples of org.exoplatform.services.rest.ApplicationContext


   public Iterator<FileItem> readFrom(Class<Iterator<FileItem>> type, Type genericType, Annotation[] annotations,
      MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
   {
      try
      {
         ApplicationContext context = ApplicationContextImpl.getCurrent();
         int bufferSize =
            context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE) == null
               ? RequestHandler.WS_RS_BUFFER_SIZE_VALUE : Integer.parseInt(context.getProperties().get(
                  RequestHandler.WS_RS_BUFFER_SIZE));
         File repo = new File(context.getProperties().get(RequestHandler.WS_RS_TMP_DIR));

         DefaultFileItemFactory factory = new DefaultFileItemFactory(bufferSize, repo);
         final FileUpload upload = new FileUpload(factory);

         return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Iterator<FileItem>>()
View Full Code Here


    * @param request See {@link GenericContainerRequest}
    * @param response See {@link GenericContainerResponse}
    */
   public void dispatch(GenericContainerRequest request, GenericContainerResponse response)
   {
      ApplicationContext context = ApplicationContextImpl.getCurrent();
      String requestPath = context.getPath(false);
      List<String> parameterValues = context.getParameterValues();

      // Get root resource
      ObjectFactory<AbstractResourceDescriptor> resourceFactory = getRootResourse(parameterValues, requestPath);

      // Take the tail of the request path, the tail will be requested path
      // for lower resources, e. g. ResourceClass -> Sub-resource method/locator
      String newRequestPath = getPathTail(parameterValues);

      // save the resource class URI in hierarchy
      context.addMatchedURI(requestPath.substring(0, requestPath.lastIndexOf(newRequestPath)));

      context.setParameterNames(resourceFactory.getObjectModel().getUriPattern().getParameterNames());

      // may thrown WebApplicationException
      Object resource = resourceFactory.getInstance(context);

      dispatch(request, response, context, resourceFactory, resource, newRequestPath);
View Full Code Here

   public Iterator<FileItem> readFrom(Class<Iterator<FileItem>> type, Type genericType, Annotation[] annotations,
      MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
   {
      try
      {
         ApplicationContext context = ApplicationContextImpl.getCurrent();
         int bufferSize =
            context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE) == null
               ? RequestHandler.WS_RS_BUFFER_SIZE_VALUE : Integer.parseInt((String)context.getProperties().get(
                  RequestHandler.WS_RS_BUFFER_SIZE));
         File repo = new File((String)context.getProperties().get(RequestHandler.WS_RS_TMP_DIR));

         DefaultFileItemFactory factory = new DefaultFileItemFactory(bufferSize, repo);
         FileUpload upload = new FileUpload(factory);
         return upload.parseRequest(httpRequest).iterator();
      }
View Full Code Here

   @SuppressWarnings("unchecked")
   public void handleRequest(GenericContainerRequest request, GenericContainerResponse response) throws Exception
   {
      try
      {
         ApplicationContext context = new ApplicationContextImpl(request, response, ProviderBinder.getInstance());
         context.getProperties().putAll(properties);
         ApplicationContextImpl.setCurrent(context);

         for (ObjectFactory<FilterDescriptor> factory : ProviderBinder.getInstance().getRequestFilters(
            context.getPath()))
         {
            RequestFilter f = (RequestFilter)factory.getInstance(context);
            f.doFilter(request);
         }

         try
         {

            dispatcher.dispatch(request, response);
            if (response.getHttpHeaders().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
            {
               String jaxrsHeader = getJaxrsHeader(response.getStatus());
               if (jaxrsHeader != null)
               {
                  response.getHttpHeaders().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
               }
            }

         }
         catch (Exception e)
         {
            if (e instanceof WebApplicationException)
            {

               Response errorResponse = ((WebApplicationException)e).getResponse();
               ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(WebApplicationException.class);

               int errorStatus = errorResponse.getStatus();
               // should be some of 4xx status
               if (errorStatus < 500)
               {
                  // Warn about error in debug mode only.
                  if (LOG.isDebugEnabled() && e.getCause() != null)
                  {
                     LOG.warn("WebApplication exception occurs.", e.getCause());
                  }
               }
               else
               {
                  if (e.getCause() != null)
                  {
                     LOG.warn("WebApplication exception occurs.", e.getCause());
                  }
               }
               // -----
               if (errorResponse.getEntity() == null)
               {
                  if (excmap != null)
                  {
                     errorResponse = excmap.toResponse(e);
                  }
                  else
                  {
                     if (e.getMessage() != null)
                     {
                        errorResponse = createErrorResponse(errorStatus, e.getMessage());
                     }
                  }
               }
               else
               {
                  if (errorResponse.getMetadata().getFirst(ExtHttpHeaders.JAXRS_BODY_PROVIDED) == null)
                  {
                     String jaxrsHeader = getJaxrsHeader(errorStatus);
                     if (jaxrsHeader != null)
                     {
                        errorResponse.getMetadata().putSingle(ExtHttpHeaders.JAXRS_BODY_PROVIDED, jaxrsHeader);
                     }
                  }
               }
               response.setResponse(errorResponse);
            }
            else if (e instanceof InternalException)
            {
               Throwable cause = e.getCause();
               Class causeClazz = cause.getClass();
               ExceptionMapper excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
               while (causeClazz != null && excmap == null)
               {
                  excmap = ProviderBinder.getInstance().getExceptionMapper(causeClazz);
                  if (excmap == null)
                     causeClazz = causeClazz.getSuperclass();
               }
               if (excmap != null)
               {
                  if (LOG.isDebugEnabled())
                  {
                     // Hide error message if exception mapper exists.
                     LOG.warn("Internal error occurs.", cause);
                  }
                  response.setResponse(excmap.toResponse(e.getCause()));
               }
               else
               {
                  LOG.error("Internal error occurs.", cause);
                  throw new UnhandledException(e.getCause());
               }
            }
            else
            {
               throw new UnhandledException(e);
            }
         }

         for (ObjectFactory<FilterDescriptor> factory : ProviderBinder.getInstance().getResponseFilters(
            context.getPath()))
         {
            ResponseFilter f = (ResponseFilter)factory.getInstance(context);
            f.doFilter(response);
         }
View Full Code Here

    * @param request See {@link GenericContainerRequest}
    * @param response See {@link GenericContainerResponse}
    */
   public void dispatch(GenericContainerRequest request, GenericContainerResponse response)
   {
      ApplicationContext context = ApplicationContextImpl.getCurrent();
      String requestPath = context.getPath(false);
      List<String> parameterValues = context.getParameterValues();

      // Get root resource
      ObjectFactory<AbstractResourceDescriptor> resourceFactory = getRootResourse(parameterValues, requestPath);

      // Take the tail of the request path, the tail will be requested path
      // for lower resources, e. g. ResourceClass -> Sub-resource method/locator
      String newRequestPath = getPathTail(parameterValues);

      // save the resource class URI in hierarchy
      context.addMatchedURI(requestPath.substring(0, requestPath.lastIndexOf(newRequestPath)));

      context.setParameterNames(resourceFactory.getObjectModel().getUriPattern().getParameterNames());

      // may thrown WebApplicationException
      Object resource = resourceFactory.getInstance(context);

      dispatch(request, response, context, resourceFactory, resource, newRequestPath);
View Full Code Here

   @SuppressWarnings("unchecked")
   public MultivaluedMap<String, String> readFrom(Class<MultivaluedMap<String, String>> type, Type genericType,
      Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders,
      InputStream entityStream) throws IOException
   {
      ApplicationContext context = ApplicationContextImpl.getCurrent();
      Object o = context.getAttributes().get("org.exoplatform.ws.rs.entity.form");
      if (o != null)
         return (MultivaluedMap<String, String>)o;

      MultivaluedMap<String, String> form = new MultivaluedMapImpl();
      int r = -1;
      StringBuffer sb = new StringBuffer();
      try
      {
         while ((r = entityStream.read()) != -1)
         {
            if (r != '&')
            {
               sb.append((char)r);
            }
            else
            {
               addPair(sb.toString().trim(), form);
               sb.setLength(0);
            }
         }
         // keep the last part
         addPair(sb.toString(), form);

         context.getAttributes().put("org.exoplatform.ws.rs.entity.form", form);

         return form;
      }
      catch (UnsupportedEncodingException e)
      {
View Full Code Here

    * @param request See {@link GenericContainerRequest}
    * @param response See {@link GenericContainerResponse}
    */
   public void dispatch(GenericContainerRequest request, GenericContainerResponse response)
   {
      ApplicationContext context = ApplicationContextImpl.getCurrent();
      String requestPath = context.getPath(false);
      List<String> parameterValues = context.getParameterValues();

      // Get root resource
      ObjectFactory<AbstractResourceDescriptor> resourceFactory = getRootResourse(parameterValues, requestPath);
      AbstractResourceDescriptor resourceDescriptor = resourceFactory.getObjectModel();

      if (providersRegistry != null)
      {
         // Be sure instance of ProvidersRegistry injected if this class is extended.
         String applicationId = null;
         if (resourceDescriptor instanceof ApplicationResource)
         {
            // If resource delivered with subclass of javax.ws.rs.core.Application
            // it must be instance of ApplicationResource which provide application's identifier.
            applicationId = ((ApplicationResource)resourceDescriptor).getApplication();
         }
         ProviderBinder applicationProviders = providersRegistry.getProviders(applicationId);
         ((ApplicationContextImpl)context).setProviders(new ProvidersAdapter(applicationProviders, ProviderBinder
            .getInstance()));
      }
      else
      {
         LOG.warn("ProvidersRegistry must set. ");
      }

      // Apply application specific request filters if any
      for (ObjectFactory<FilterDescriptor> factory : context.getProviders().getRequestFilters(context.getPath()))
      {
         RequestFilter f = (RequestFilter)factory.getInstance(context);
         f.doFilter(request);
      }

      // Take the tail of the request path, the tail will be requested path
      // for lower resources, e. g. ResourceClass -> Sub-resource method/locator
      String newRequestPath = getPathTail(parameterValues);
      // save the resource class URI in hierarchy
      context.addMatchedURI(requestPath.substring(0, requestPath.lastIndexOf(newRequestPath)));
      context.setParameterNames(resourceFactory.getObjectModel().getUriPattern().getParameterNames());

      // may thrown WebApplicationException
      Object resource = resourceFactory.getInstance(context);
      dispatch(request, response, context, resourceFactory, resource, newRequestPath);

      // Apply application specific response filters if any
      for (ObjectFactory<FilterDescriptor> factory : context.getProviders().getResponseFilters(context.getPath()))
      {
         ResponseFilter f = (ResponseFilter)factory.getInstance(context);
         f.doFilter(response);
      }
   }
View Full Code Here

   {

      boolean overflow = false;
      byte[] buffer = new byte[8192];

      ApplicationContext context = ApplicationContextImpl.getCurrent();
      int bufferSize =
         context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE) == null ? RequestHandler.WS_RS_BUFFER_SIZE_VALUE
            : Integer.parseInt((String)context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE));
      ByteArrayOutputStream bout = new ByteArrayOutputStream(bufferSize);

      int bytes = -1;
      while ((bytes = entityStream.read(buffer)) != -1)
      {
View Full Code Here

   public Iterator<FileItem> readFrom(Class<Iterator<FileItem>> type, Type genericType, Annotation[] annotations,
      MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException
   {
      try
      {
         ApplicationContext context = ApplicationContextImpl.getCurrent();
         int bufferSize =
            context.getProperties().get(RequestHandler.WS_RS_BUFFER_SIZE) == null
               ? RequestHandler.WS_RS_BUFFER_SIZE_VALUE : Integer.parseInt(context.getProperties().get(
                  RequestHandler.WS_RS_BUFFER_SIZE));
         File repo = new File(context.getProperties().get(RequestHandler.WS_RS_TMP_DIR));

         DefaultFileItemFactory factory = new DefaultFileItemFactory(bufferSize, repo);
         final FileUpload upload = new FileUpload(factory);

         return SecurityHelper.doPrivilegedExceptionAction(new PrivilegedExceptionAction<Iterator<FileItem>>()
View Full Code Here

      {
         responseWriter.writeHeaders(this);
         return;
      }

      ApplicationContext context = ApplicationContextImpl.getCurrent();
      MediaType contentType = getContentType();

      // if content-type is still not preset try determine it
      if (contentType == null)
      {
         List<MediaType> l = context.getProviders().getAcceptableWriterMediaTypes(entity.getClass(), entityType, null);
         contentType = context.getContainerRequest().getAcceptableMediaType(l);
         if (contentType == null || contentType.isWildcardType() || contentType.isWildcardSubtype())
            contentType = MediaType.APPLICATION_OCTET_STREAM_TYPE;

         this.contentType = contentType;
         getHttpHeaders().putSingle(HttpHeaders.CONTENT_TYPE, contentType);
      }
      MessageBodyWriter entityWriter =
         context.getProviders().getMessageBodyWriter(entity.getClass(), entityType, null, contentType);
      if (entityWriter == null)
      {
         String message = "Not found writer for " + entity.getClass() + " and MIME type " + contentType;
         if (context.getContainerRequest().getMethod().equals(HttpMethod.HEAD))
         {
            // just warning here, HEAD method we do not need write entity
            LOG.warn(message);
            getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(-1));
         }
         else
         {
            LOG.error(message);
            throw new WebApplicationException(Response.status(Response.Status.NOT_ACCEPTABLE).build());
         }
      }
      else
      {
         if (getHttpHeaders().getFirst(HttpHeaders.CONTENT_LENGTH) == null)
         {
            long contentLength = entityWriter.getSize(entity, entity.getClass(), entityType, null, contentType);
            if (contentLength >= 0)
               getHttpHeaders().putSingle(HttpHeaders.CONTENT_LENGTH, Long.toString(contentLength));
         }
      }
      if (context.getContainerRequest().getMethod().equals(HttpMethod.HEAD))
         entity = null;

      responseWriter.writeHeaders(this);
      responseWriter.writeBody(this, entityWriter);
   }
View Full Code Here

TOP

Related Classes of org.exoplatform.services.rest.ApplicationContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.