Coverage Report - org.apache.johnzon.jaxrs.JohnzonMessageBodyWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
JohnzonMessageBodyWriter
80%
12/15
62%
15/24
3,2
 
 1  
 /*
 2  
  * Licensed to the Apache Software Foundation (ASF) under one
 3  
  * or more contributor license agreements. See the NOTICE file
 4  
  * distributed with this work for additional information
 5  
  * regarding copyright ownership. The ASF licenses this file
 6  
  * to you under the Apache License, Version 2.0 (the
 7  
  * "License"); you may not use this file except in compliance
 8  
  * with the License. You may obtain a copy of the License at
 9  
  *
 10  
  * http://www.apache.org/licenses/LICENSE-2.0
 11  
  *
 12  
  * Unless required by applicable law or agreed to in writing,
 13  
  * software distributed under the License is distributed on an
 14  
  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 15  
  * KIND, either express or implied. See the License for the
 16  
  * specific language governing permissions and limitations
 17  
  * under the License.
 18  
  */
 19  
 package org.apache.johnzon.jaxrs;
 20  
 
 21  
 import org.apache.johnzon.mapper.Mapper;
 22  
 import org.apache.johnzon.mapper.MapperBuilder;
 23  
 
 24  
 import javax.json.JsonStructure;
 25  
 import javax.ws.rs.Produces;
 26  
 import javax.ws.rs.core.MediaType;
 27  
 import javax.ws.rs.core.MultivaluedMap;
 28  
 import javax.ws.rs.core.Response;
 29  
 import javax.ws.rs.core.StreamingOutput;
 30  
 import javax.ws.rs.ext.MessageBodyWriter;
 31  
 import javax.ws.rs.ext.Provider;
 32  
 import java.io.IOException;
 33  
 import java.io.InputStream;
 34  
 import java.io.OutputStream;
 35  
 import java.io.Writer;
 36  
 import java.lang.annotation.Annotation;
 37  
 import java.lang.reflect.ParameterizedType;
 38  
 import java.lang.reflect.Type;
 39  
 import java.util.Collection;
 40  
 
 41  
 import static javax.ws.rs.core.MediaType.WILDCARD;
 42  
 
 43  
 @Provider
 44  
 @Produces(WILDCARD)
 45  
 public class JohnzonMessageBodyWriter<T> extends IgnorableTypes implements MessageBodyWriter<T> {
 46  
     private final Mapper mapper;
 47  
 
 48  
     public JohnzonMessageBodyWriter() {
 49  0
         this(new MapperBuilder().setDoCloseOnStreams(false).build(), null);
 50  0
     }
 51  
 
 52  
     public JohnzonMessageBodyWriter(final Mapper mapper, final Collection<String> ignoredTypes) {
 53  10
         super(ignoredTypes);
 54  10
         this.mapper = mapper;
 55  10
     }
 56  
 
 57  
     @Override
 58  
     public long getSize(final T t, final Class<?> rawType, final Type genericType,
 59  
                         final Annotation[] annotations, final MediaType mediaType) {
 60  0
         return -1;
 61  
     }
 62  
 
 63  
     @Override
 64  
     public boolean isWriteable(final Class<?> rawType, final Type genericType,
 65  
                                final Annotation[] annotations, final MediaType mediaType) {
 66  10
         return Jsons.isJson(mediaType)
 67  10
                 && !isIgnored(rawType)
 68  
                 && InputStream.class != rawType
 69  
                 && OutputStream.class != rawType
 70  
                 && Writer.class != rawType
 71  
                 && StreamingOutput.class != rawType
 72  
                 && String.class != rawType
 73  
                 && Response.class != rawType
 74  8
                 && !JsonStructure.class.isAssignableFrom(rawType);
 75  
     }
 76  
 
 77  
     @Override
 78  
     public void writeTo(final T t, final Class<?> rawType, final Type genericType,
 79  
                         final Annotation[] annotations, final MediaType mediaType,
 80  
                         final MultivaluedMap<String, Object> httpHeaders,
 81  
                         final OutputStream entityStream) throws IOException {
 82  8
         if (rawType.isArray()) {
 83  2
             mapper.writeArray(t, entityStream);
 84  6
         } else if (Collection.class.isAssignableFrom(rawType) && ParameterizedType.class.isInstance(genericType)) {
 85  2
             mapper.writeArray(Collection.class.cast(t), entityStream);
 86  
         } else {
 87  4
             mapper.writeObject(t, entityStream);
 88  
         }
 89  8
     }
 90  
 }