Java tutorial
/** * PureInfo Command * @(#)CodeGetActionBase.java 1.0 2006-11-13 * * Copyright(c) 2004-2005, PureInfo Information Technology Corp. Ltd. * All rights reserved, see the license file. * * www.pureinfo.com.cn */ package com.pureinfo.srm.action; import java.io.IOException; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForward; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.XMLWriter; import com.pureinfo.ark.interaction.ActionBase; import com.pureinfo.force.exception.PureException; import com.pureinfo.srm.project.CodeInfo; import com.pureinfo.srm.project.ICodeGnerator; /** * <P> * Created on 2006-11-13 16:08:23 <BR> * Last modified on 2006-11-13 * </P> * TODO describe CodeGetActionBase here ... * * @author elmar.chen * @version 1.0, 2006-11-13 * @since Command 1.0 */ public abstract class CodeGetActionBase extends ActionBase { protected boolean isLoginNeeded() { return false; } /** * @see com.pureinfo.ark.interaction.ActionBase#executeAction() */ public ActionForward executeAction() throws PureException { try { Document xDoc; try { ICodeGnerator cg = getGenerator(); CodeInfo info = cg.getNextCode(getParams()); xDoc = wrapInfo(info); } catch (Exception e) { e.printStackTrace(); xDoc = createErrorXML(); } try { response.setCharacterEncoding("utf-8"); response.setContentType("text/xml"); response.setHeader("Cache-Control", "no-cache"); //HTTP 1.1 response.setHeader("Pragma", "no-cache"); //HTTP 1.0 response.setDateHeader("Expires", -1); response.setDateHeader("max-age", 0); XMLWriter writer = new XMLWriter(response.getWriter()); writer.write(xDoc); writer.close(); } catch (IOException ex1) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); } return null; } catch (RuntimeException e) { return mapping.findForward("code-get-error"); } } /** * @return */ private Document createErrorXML() { Document doc = DocumentHelper.createDocument(); Element xRoot = doc.addElement("responseXML"); addInfo(xRoot, "success", "false"); addInfo(xRoot, "value", "ERROR"); addInfo(xRoot, "msg", ""); return doc; } /** * @param _info * @return */ private Document wrapInfo(CodeInfo _info) { Document doc = DocumentHelper.createDocument(); Element xRoot = doc.addElement("responseXML"); addInfo(xRoot, "success", "true"); addInfo(xRoot, "value", _info.value); if (_info.cross_boundary) { String sMsg = " " + _info.round + " "; addInfo(xRoot, "msg", sMsg); } return doc; } private void addInfo(Element _xRoot, String _sKey, String _sValue) { Element xValue = _xRoot.addElement(_sKey); xValue.setText(_sValue); } protected abstract ICodeGnerator getGenerator(); protected abstract Object[] getParams(); }