Coverage Report - org.apache.johnzon.jaxrs.WadlDocumentMessageBodyWriter
 
Classes in this File Line Coverage Branch Coverage Complexity
WadlDocumentMessageBodyWriter
0%
0/9
0%
0/4
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.jaxrs.xml.WadlDocumentToJson;
 22  
 import org.w3c.dom.Document;
 23  
 
 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.MessageBodyWriter;
 28  
 import javax.xml.stream.XMLStreamException;
 29  
 import java.io.IOException;
 30  
 import java.io.OutputStream;
 31  
 import java.lang.annotation.Annotation;
 32  
 import java.lang.reflect.Type;
 33  
 
 34  
 import static org.apache.johnzon.jaxrs.Jsons.isJson;
 35  
 
 36  0
 public class WadlDocumentMessageBodyWriter implements MessageBodyWriter<Document> {
 37  0
     private final WadlDocumentToJson converter = new WadlDocumentToJson();
 38  
 
 39  
     @Override
 40  
     public boolean isWriteable(final Class<?> aClass, final Type type,
 41  
                                final Annotation[] annotations, final MediaType mediaType) {
 42  0
         return isJson(mediaType) && Document.class.isAssignableFrom(aClass);
 43  
     }
 44  
 
 45  
     @Override
 46  
     public long getSize(final Document document, final Class<?> aClass,
 47  
                         final Type type, final Annotation[] annotations,
 48  
                         final MediaType mediaType) {
 49  0
         return -1;
 50  
     }
 51  
 
 52  
     @Override
 53  
     public void writeTo(final Document document, final Class<?> aClass,
 54  
                         final Type type, final Annotation[] annotations,
 55  
                         final MediaType mediaType, final MultivaluedMap<String, Object> stringObjectMultivaluedMap,
 56  
                         final OutputStream outputStream) throws IOException, WebApplicationException {
 57  
         try {
 58  0
             outputStream.write(converter.convert(document).getBytes());
 59  0
         } catch (final XMLStreamException e) {
 60  0
             throw new IllegalStateException(e);
 61  0
         }
 62  0
     }
 63  
 }