001    package org.deegree.enterprise.control.ajax;
002    
003    import java.io.IOException;
004    
005    import java.io.OutputStream;
006    import java.nio.charset.Charset;
007    import java.util.Locale;
008    
009    import javax.servlet.http.HttpServletResponse;
010    import javax.xml.transform.Source;
011    import javax.xml.transform.TransformerException;
012    import javax.xml.transform.dom.DOMSource;
013    import javax.xml.transform.stream.StreamResult;
014    
015    import org.deegree.framework.log.ILogger;
016    import org.deegree.framework.log.LoggerFactory;
017    import org.deegree.framework.util.CharsetUtils;
018    import org.deegree.framework.xml.XMLFragment;
019    import org.deegree.framework.xml.XSLTDocument;
020    import org.stringtree.json.JSONWriter;
021    
022    /**
023     * 
024     * 
025     * 
026     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
027     * @author last edited by: $Author: apoth $
028     * 
029     * @version. $Revision: 29086 $, $Date: 2011-01-06 14:44:01 +0100 (Thu, 06 Jan 2011) $
030     */
031    public class ResponseHandler {
032    
033        private static final ILogger LOG = LoggerFactory.getLogger( ResponseHandler.class );
034    
035        private HttpServletResponse response;
036    
037        private OutputStream os;
038    
039        private XSLTDocument xslt;
040    
041        /**
042         * 
043         * @param response
044         */
045        ResponseHandler( HttpServletResponse response ) {
046            this.response = response;
047        }
048    
049        private void openStream()
050                                throws IOException {
051            if ( os == null ) {
052                os = response.getOutputStream();
053            }
054        }
055    
056        /**
057         * sets content type
058         * 
059         * @param contentType
060         */
061        public void setContentType( String contentType ) {
062            response.setContentType( contentType );
063        }
064    
065        /**
066         * sets locale/character encoding
067         * 
068         * @param locale
069         */
070        public void setLocale( Locale locale ) {
071            response.setLocale( locale );
072        }
073    
074        /**
075         * sets content length
076         * 
077         * @param length
078         */
079        public void setContentLength( int length ) {
080            response.setContentLength( length );
081        }
082    
083        /**
084         * sets an xslt script that will be used if a XML document will be written; - {@link #writeAndClose(XMLFragment)}
085         * 
086         * @param xslt
087         */
088        void setXSLT( XSLTDocument xslt ) {
089            this.xslt = xslt;
090        }
091    
092        /**
093         * write a string result back to the client and closes the output stream. 'text/plain; charset=utf-8' will be used
094         * as default if no content-type has been set.
095         * 
096         * @param value
097         * @throws IOException
098         */
099        public void writeAndClose( String value )
100                                throws IOException {
101            if ( response.getContentType() == null ) {
102                response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() );
103            }
104            openStream();
105            byte[] b = value.getBytes();
106            setContentLength( b.length );
107            os.write( b );
108            os.flush();
109            os.close();
110        }
111    
112        /**
113         * write a XML result back to the client (if a xslt script has been set the document will be transformed first) and
114         * closes the output stream.<br>
115         * 'text/plain; charset=utf-8' will be used as default if no content-type has been set.
116         * 
117         * @param value
118         * @throws IOException
119         */
120        public void writeAndClose( XMLFragment value )
121                                throws IOException {
122            if ( response.getContentType() == null ) {
123                response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() );
124            }
125            openStream();
126    
127            if ( xslt != null ) {
128                Source xmlSource = new DOMSource( value.getRootElement() );
129                Source xslSource = new DOMSource( xslt.getRootElement() );
130                try {
131                    XSLTDocument.transform( xmlSource, xslSource, new StreamResult( os ), null, null );
132                } catch ( TransformerException e ) {
133                    LOG.logError( e.getMessage(), e );
134                    throw new IOException( e.getMessage() );
135                }
136            } else {
137                value.write( os );
138            }
139            os.flush();
140            os.close();
141        }
142    
143        /**
144         * 'application/json; charset=$deafultCharset$' will be used as default if no content-type has been set.
145         * 
146         * @param emitClassName
147         * @param value
148         * @throws IOException
149         */
150        public void writeAndClose( boolean emitClassName, Object value )
151                                throws IOException {
152    
153            if ( response.getContentType() == null ) {
154                response.setContentType( "application/json; charset=" + Charset.defaultCharset().displayName() );
155            }
156            value.getClass().getModifiers();
157            JSONWriter writer = new JSONWriter( emitClassName );
158            writeAndClose( writer.write( value ) );
159        }
160        
161        /**
162         * 
163         * @return original HttpServletResponse object
164         */
165        public HttpServletResponse getHttpServletResponse() {
166            return response;
167        }
168    
169    }