StocksProvider.java

  1. /**
  2.  * Copyright 2012 the original author or authors
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *         http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16. package com.googlecode.phisix.api.ext;

  17. import java.io.BufferedReader;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.InputStreamReader;
  21. import java.io.Reader;
  22. import java.lang.annotation.Annotation;
  23. import java.lang.reflect.Type;

  24. import javax.ws.rs.WebApplicationException;
  25. import javax.ws.rs.core.MediaType;
  26. import javax.ws.rs.core.MultivaluedMap;
  27. import javax.ws.rs.ext.MessageBodyReader;
  28. import javax.ws.rs.ext.Provider;

  29. import org.jboss.resteasy.util.NoContent;

  30. import com.googlecode.phisix.api.model.Stocks;
  31. import com.googlecode.phisix.api.parser.GsonAwareParser;
  32. import com.googlecode.phisix.api.parser.Parser;

  33. /**
  34.  * {@link MessageBodyReader} that converts an {@link InputStream} to
  35.  * {@link Stocks} via {@link Parser}
  36.  *
  37.  * @author Edge Dalmacio
  38.  *
  39.  */
  40. @Provider
  41. public class StocksProvider implements MessageBodyReader<Stocks> {

  42.     private final Parser<Reader, Stocks> parser;
  43.    
  44.     public StocksProvider() {
  45.         this(new GsonAwareParser());
  46.     }

  47.     public StocksProvider(Parser<Reader, Stocks> parser) {
  48.         this.parser = parser;
  49.     }

  50.     @Override
  51.     public boolean isReadable(Class<?> type, Type genericType,
  52.             Annotation[] annotations, MediaType mediaType) {
  53.         return type.equals(Stocks.class);
  54.     }

  55.     @Override
  56.     public Stocks readFrom(Class<Stocks> type, Type genericType,
  57.             Annotation[] annotations, MediaType mediaType,
  58.             MultivaluedMap<String, String> httpHeaders, InputStream entityStream)
  59.             throws IOException, WebApplicationException {
  60.        
  61.         if (NoContent.isContentLengthZero(httpHeaders)) {
  62.             return new Stocks();
  63.         }
  64.        
  65.         String charset = mediaType.getParameters().get("charset");
  66.        
  67.         Reader isr = charset == null
  68.                 ? new InputStreamReader(entityStream)
  69.                 : new InputStreamReader(entityStream, charset);
  70.        
  71.         Reader br = new BufferedReader(isr);
  72.        
  73.         try {
  74.             return parser.parse(br);
  75.         } finally {
  76.             br.close();
  77.         }
  78.     }

  79. }