1 19 20 package com.sslexplorer.core.actions; 21 22 import java.io.IOException ; 23 24 import javax.servlet.http.HttpServletResponse ; 25 26 import org.apache.struts.action.Action; 27 import org.jdom.Attribute; 28 import org.jdom.Document; 29 import org.jdom.Element; 30 import org.jdom.output.XMLOutputter; 31 32 public abstract class XMLOutputAction 33 extends Action { 34 35 public XMLOutputAction() { 36 } 37 38 protected void sendError(String message, 39 HttpServletResponse response) throws IOException { 40 41 Element root = new Element("error"); 42 Document doc = new Document(root); 43 root.setText(message); 44 sendDocument(doc, response); 45 46 } 47 48 protected void sendDocument(Document doc, 49 HttpServletResponse response) throws IOException { 50 response.setHeader("Content-type", "text/xml"); 51 XMLOutputter o = new XMLOutputter(); 52 o.output(doc, response.getOutputStream()); 53 response.getOutputStream().close(); 54 55 } 56 57 protected void sendSuccess(String content, 58 HttpServletResponse response) throws IOException { 59 sendSuccess(content, response, null); 60 } 61 62 protected void sendSuccess(String content, 63 HttpServletResponse response, 64 Attribute[] attributes) throws IOException { 65 66 Element root = new Element("success"); 67 68 for (int i = 0; attributes != null && i < attributes.length; i++) { 69 root.setAttribute(attributes[i]); 70 71 } 72 Document doc = new Document(root); 73 root.setText(content); 74 sendDocument(doc, response); 75 76 } 77 78 } | Popular Tags |