001 package com.khubla.pragmatach.plugin.xstream; 002 003 import java.io.ByteArrayInputStream; 004 import java.util.Map; 005 006 import javax.servlet.http.HttpServletResponse; 007 008 import org.apache.commons.io.IOUtils; 009 010 import com.khubla.pragmatach.framework.api.PragmatachException; 011 import com.khubla.pragmatach.framework.controller.PragmatachController; 012 import com.khubla.pragmatach.framework.controller.impl.AbstractController; 013 import com.khubla.pragmatach.framework.controller.impl.AbstractResponse; 014 import com.thoughtworks.xstream.XStream; 015 016 /** 017 * @author tome 018 */ 019 public class XStreamResponse extends AbstractResponse { 020 /** 021 * the controller 022 */ 023 private final PragmatachController pragmatachController; 024 025 /** 026 * ctor 027 */ 028 public XStreamResponse(Map<String, String> cacheHeaders, PragmatachController pragmatachController) { 029 super(cacheHeaders); 030 this.pragmatachController = pragmatachController; 031 } 032 033 @Override 034 public String getContentType() throws PragmatachException { 035 return "text/xml"; 036 } 037 038 @Override 039 public Map<String, String> getHeaders() throws PragmatachException { 040 return super.getCacheHeaders(); 041 } 042 043 @Override 044 public void render(HttpServletResponse httpServletResponse) throws PragmatachException { 045 try { 046 final XStream xstream = new XStream(); 047 xstream.omitField(AbstractController.class, "request"); 048 final String XML = xstream.toXML(pragmatachController); 049 final ByteArrayInputStream bais = new ByteArrayInputStream(XML.getBytes()); 050 IOUtils.copy(bais, httpServletResponse.getOutputStream()); 051 } catch (final Exception e) { 052 throw new PragmatachException("Exception in render", e); 053 } 054 } 055 }