content: package org.agoncal.book.javaee7.chapter15.ex16;
  
  import javax.ws.rs.Produces;
  import javax.ws.rs.WebApplicationException;
  import javax.ws.rs.core.MediaType;
  import javax.ws.rs.core.MultivaluedMap;
  import javax.ws.rs.ext.MessageBodyWriter;
  import javax.ws.rs.ext.Provider;
  import java.io.IOException;
  import java.io.OutputStream;
  import java.lang.annotation.Annotation;
  import java.lang.reflect.Type;
  
  /**
   * @author Antonio Goncalves
   *         APress Book - Beginning Java EE 7 with Glassfish 4
   *         http://www.apress.com/
   *         http://www.antoniogoncalves.org
   *         --
   */
  @Provider
  @Produces("custom/format")
  public class CustomCustomerWriter16 implements MessageBodyWriter<Customer16> {
  
    // ======================================
    // =           Public Methods           =
    // ======================================
  
    @Override
    public boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
      return Customer16.class.isAssignableFrom(type);
    }
  
    @Override
    public void writeTo(Customer16 customer, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream outputStream) throws IOException, WebApplicationException {
      outputStream.write(customer.getId().getBytes());
      outputStream.write('/');
      outputStream.write(customer.getFirstName().getBytes());
      outputStream.write('/');
      outputStream.write(customer.getLastName().getBytes());
    }
  
    @Override
    public long getSize(Customer16 customer, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
      return customer.getId().length() + 1 + customer.getFirstName().length() + 1 + customer.getLastName().length();
    }
  }
  