package com.nbtconsulting.demo.restws.v2007.model.web.view;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.View;
import org.apache.commons.io.IOUtils;
import java.io.OutputStream;
//---------------------------------------------------------
//---------------------------------------------------------
/**
Output text (from a String) as a view representation.
This view will look for a key named
"content"
in the model.
This view will use the value for a key named
"content"
in the model as the view representation.
Example Groovy code:
StringBuffer sbuff = new StringBuffer()
sbuff << "some message here"
View view = new XmlView()
Map myMap = [:]
myMap["text"] = sbuff.toString
ModelAndView mav = new ModelAndView(view, myMap)
return mav
*/
public class XmlView
implements View
{
//Log log = LogFactory.getLog(XmlView.class);
static org.apache.commons.logging.Log logger =
org.apache.commons.logging.LogFactory.getLog(XmlView.class);
//protected static String STR_DEFAULT_CONTENT_TYPE = "text/xml";
protected static String STR_DEFAULT_CONTENT_TYPE = "application/xml";
protected String strContentType = STR_DEFAULT_CONTENT_TYPE;
//---------------------------------------------------------
public String getContentType()
{
return strContentType;
}
//---------------------------------------------------------
public void setContentType(String strContentType)
{
this.strContentType = strContentType;
}
//---------------------------------------------------------
/**
Will look for the value (view content) with the key of "content"
or "xml" or "text" (in order) in the model.
The content will be sent to the HttpServletResponse output.
*/
public void render(Map model, HttpServletRequest request,
HttpServletResponse response) throws Exception
{
logger.debug("entered");
Object objResponseContent = "";
if (model != null)
{
// Retrieve data from model
objResponseContent = model.get("content");
/*
if (objResponseContent == null)
{
objResponseContent = model.get("xml");
}
if (objResponseContent == null)
{
objResponseContent = model.get("text");
}
*/
if (objResponseContent == null)
{
objResponseContent = "";
}
}
response.setContentType(getContentType());
// Write the content document to the HttpServletResponse output stream
OutputStream out = response.getOutputStream();
if (objResponseContent instanceof byte[])
{
IOUtils.write((byte[]) objResponseContent, out);
response.setContentLength(((byte[]) objResponseContent).length);
}
else if (objResponseContent instanceof String)
{
IOUtils.write((String) objResponseContent, out);
response.setContentLength(((String) objResponseContent).length());
}
else if (objResponseContent instanceof StringBuffer)
{
IOUtils.write((StringBuffer) objResponseContent, out);
response.setContentLength(((StringBuffer) objResponseContent).length());
}
else if (objResponseContent instanceof char[])
{
IOUtils.write((char[]) objResponseContent, out);
response.setContentLength(((char[]) objResponseContent).length);
}
else
{
throw new RuntimeException("content type is not handled by this view");
}
}
}