/* * Created on 27.11.2003 * * (c) Stabsstelle IKT-Strategie des Bundes */ package at.gv.egovernment.moa.spss.slinterface.beans; import iaik.utils.Util; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Properties; import java.util.Random; import javax.servlet.ServletContext; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpSessionBindingEvent; import javax.servlet.http.HttpSessionBindingListener; import org.apache.log4j.Logger; import org.w3c.dom.Document; import org.w3c.dom.Element; import at.gv.egovernment.moa.spss.slinterface.Constants; import at.gv.egovernment.moa.spss.slinterface.DOMUtils; /** * @author Gregor Karlinger (mailto:gregor.karlinger@cio.gv.at) */ public class DataInfoBean implements HttpSessionBindingListener { private static Logger logger_ = Logger.getLogger(Constants.LH_BEANS_); private static final String HID_ELEM_ = "HashInputData"; private static final String B64CONT_ELEM_ = "Base64Content"; private static final String XMLCONT_ELEM_ = "XMLContent"; private static final String HID_URL_PREFIX_ = "/showdata?hidCount="; List hashInputDataFilenames_; int hashInputDataCount_; /* ---------------------------------------------------------------------------------------------------- */ public DataInfoBean(Document moaResponseDoc, ServletContext context, HttpSession session) throws IOException { hashInputDataFilenames_ = new ArrayList(); int hashInputDataCount_ = 0; Element moaResponseElem = moaResponseDoc.getDocumentElement(); List hidElems = DOMUtils.getChildElems(moaResponseElem, Constants.NSURI_MOA_12_, HID_ELEM_, false); Properties initProps = (Properties) context.getAttribute(Constants.WSCP_INIT_PROPS_); String tempDir = initProps.getProperty(Constants.IP_TEMP_DIR_); if (tempDir == null) { String message = "Init property \"" + Constants.IP_TEMP_DIR_ + "\" not set."; logger_.error(message); throw new IOException(message); } Random random = new Random(); for (int i = 0; i < hidElems.size(); i++) { // Open file for current hash input data String currHidFileNameStr = tempDir + session.getId() + "_" + System.currentTimeMillis() + "_" + random.nextLong(); currHidFileNameStr = context.getRealPath(currHidFileNameStr); FileOutputStream currHidFOS; try { currHidFOS = new FileOutputStream(currHidFileNameStr); } catch (IOException e) { String message = "Cannot open file \"" + currHidFileNameStr + "\"."; logger_.error(message); throw new IOException(message); } // Write HID to file Element currHidElem = (Element) hidElems.get(i); Element base64ContentElem = DOMUtils.getChildElem(currHidElem, Constants.NSURI_MOA_12_, B64CONT_ELEM_); if (base64ContentElem != null) { // HID is base64 String base64ContentText = DOMUtils.getText(base64ContentElem); byte[] content = Util.Base64Decode(base64ContentText.getBytes()); try { currHidFOS.write(content); currHidFOS.close(); } catch (IOException e) { String message = "Cannot write to file \"" + currHidFileNameStr + "\"."; logger_.error(message); throw new IOException(message); } } else { // HID is XML // TODO treatment of XML content throw new RuntimeException("XML content not support yet."); } hashInputDataFilenames_.add(currHidFileNameStr); } } /* ---------------------------------------------------------------------------------------------------- */ public void valueBound(HttpSessionBindingEvent event) { // Do nothing. } /* ---------------------------------------------------------------------------------------------------- */ public void valueUnbound(HttpSessionBindingEvent event) { // Delete all temporary hash input data files for (int i = 0; i < hashInputDataFilenames_.size(); i++) { String currFileStr = (String) hashInputDataFilenames_.get(i); File currFile = new File(currFileStr); currFile.delete(); } } /* ---------------------------------------------------------------------------------------------------- */ public void setHashInputDataCount(int count) { hashInputDataCount_ = count; } /* ---------------------------------------------------------------------------------------------------- */ public String getHashInputDataFilename() { return (String) hashInputDataFilenames_.get(hashInputDataCount_); } /* ---------------------------------------------------------------------------------------------------- */ public String getHashInputDataURL() { return (hashInputDataFilenames_.size() > hashInputDataCount_) ? HID_URL_PREFIX_ + hashInputDataCount_ : null; } }