1 23 package org.openharmonise.dav.server.webservice; 24 25 26 import java.io.File ; 27 import java.io.FileInputStream ; 28 import java.io.FileOutputStream ; 29 import java.io.IOException ; 30 import java.rmi.RemoteException ; 31 import java.util.Iterator ; 32 import java.util.zip.Deflater ; 33 import java.util.zip.ZipEntry ; 34 import java.util.zip.ZipOutputStream ; 35 36 import javax.xml.parsers.FactoryConfigurationError ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 39 import org.openharmonise.commons.dsi.*; 40 import org.openharmonise.commons.net.Email; 41 import org.openharmonise.commons.xml.*; 42 import org.openharmonise.commons.xml.namespace.NamespaceType; 43 import org.openharmonise.dav.server.utils.*; 44 import org.openharmonise.rm.DataAccessException; 45 import org.openharmonise.rm.config.ConfigException; 46 import org.openharmonise.rm.config.ConfigSettings; 47 import org.openharmonise.rm.dsi.DataStoreInterfaceFactory; 48 import org.openharmonise.rm.factory.HarmoniseFactoryException; 49 import org.openharmonise.rm.factory.HarmoniseObjectFactory; 50 import org.openharmonise.rm.publishing.*; 51 import org.openharmonise.rm.resources.AbstractChildObject; 52 import org.openharmonise.rm.resources.AbstractParentObject; 53 import org.openharmonise.rm.resources.content.Section; 54 import org.openharmonise.rm.resources.lifecycle.Status; 55 import org.openharmonise.rm.resources.metadata.properties.PropertyGroup; 56 import org.openharmonise.rm.resources.publishing.WebPage; 57 import org.w3c.dom.*; 58 import org.xml.sax.SAXException ; 59 68 public class PublishService { 69 private static final String ATTRIB_HREF = "href"; 70 protected AbstractDataStoreInterface m_dsi = null; 71 static final String TAG_HARMONISE_PUBLISH_SERVICE = "HarmonisePublishService"; 72 static final String PNAME_UPLOADED_TEMPFILE_DIR = "UPLOADED_TEMPFILE_DIR"; 73 static final String PNAME_EMAIL_HOST = "EMAIL_HOST"; 74 static final String DOCUMENT_ROOT_PATH = "/root/documents"; 75 static final String PROP_GROUP_ROOT_PATH = "/root/props"; 76 77 83 public String publish(String sXml) throws RemoteException { 84 String sResult = null; 85 try { 86 Document doc = XMLDocument.getXMLDocumentFromString(sXml); 87 Element inputRoot = doc.getDocumentElement(); 88 String sTagName = inputRoot.getTagName(); 89 if (sTagName.equals(TAG_HARMONISE_PUBLISH_SERVICE) == false) { 90 throw new RemoteException ( 91 "Root element must be " + TAG_HARMONISE_PUBLISH_SERVICE); 92 } 93 convertWebdavPaths(doc); 94 NodeList nodes = inputRoot.getElementsByTagName(State.TAG_STATE); 95 Element stateEl = (Element) nodes.item(0); 96 State state = null; 97 if (stateEl != null) { 98 Document stateXml = new XMLDocument(); 99 stateXml.appendChild(XMLUtils.copyNode(stateEl, stateXml)); 100 state = new State(stateXml, getDataStoreInterface()); 101 } else { 102 state = new State(getDataStoreInterface()); 103 Element el = state.createElement(State.TAG_STATE); 104 state.appendChild(el); 105 } 106 nodes = inputRoot.getElementsByTagName(WebPage.TAG_HARMONISE); 107 Element ohEl = (Element) nodes.item(0); 108 WebPageEngine engine = 109 new WebPageEngine(this.getDataStoreInterface()); 110 HarmoniseOutput out = null; 111 WebPage page = null; 112 if (ohEl != null) { 113 out = engine.createXML(ohEl, state); 114 } else { 115 nodes = stateEl.getElementsByTagName(WebPage.TAG_PAGE); 116 Element pageEl = (Element) nodes.item(0); 117 if (pageEl != null) { 118 String sId = pageEl.getAttribute(WebPage.ATTRIB_ID); 119 if (sId != null) { 120 int nId = Integer.parseInt(sId); 121 page = new WebPage(getDataStoreInterface(), nId); 122 System.out.println("Found page " + page.getName()); 123 } 124 } 125 out = engine.createXML(page, state); 126 } 127 XMLPrettyPrint printer = new XMLPrettyPrint(); 128 printer.setNamespaceAware(true); 129 Element outEL = out.getDocumentElement(); 130 sResult = printer.printNode(outEL); 131 } catch (Exception e) { 132 throw new RemoteException (e.getLocalizedMessage()); 133 } 134 return sResult; 135 } 136 137 public void publishContent(String sEmail, boolean bShowContent, boolean bShowMetadata) throws RemoteException { 138 String sResult = null; 139 try { 140 String sTempDir = ConfigSettings.getProperty(PNAME_UPLOADED_TEMPFILE_DIR); 141 String sZipPath = sTempDir + "/HarmoniseContent.zip"; 142 FileOutputStream fos = new FileOutputStream (sZipPath); 143 ZipOutputStream zos = new ZipOutputStream (fos); 144 zos.setLevel(Deflater.DEFAULT_COMPRESSION); 145 146 if(bShowContent) { 147 String sHarmonisePath = sTempDir + "/HarmoniseContent"; 148 149 File harmoniseDir = new File (sHarmonisePath); 150 harmoniseDir.mkdir(); 151 152 AbstractParentObject docRoot = (AbstractParentObject) HarmoniseObjectFactory.instantiateHarmoniseObject(getDataStoreInterface(),Section.class.getName(), DOCUMENT_ROOT_PATH); 153 writeCollection(docRoot, harmoniseDir); 154 155 recurseDir(zos, sHarmonisePath); 156 157 harmoniseDir.delete(); 158 } 159 160 if(bShowMetadata) { 161 HarmoniseOutput out = new HarmoniseOutput(getDataStoreInterface()); 162 163 Element propGroupEl = out.createElement("Metadata"); 164 out.appendChild(propGroupEl); 165 166 PropertyGroup propGroupRoot = (PropertyGroup) HarmoniseObjectFactory.instantiateHarmoniseObject(getDataStoreInterface(),PropertyGroup.class.getName(), PROP_GROUP_ROOT_PATH); 167 writePropertyGroup(propGroupRoot, out.getDocumentElement(), out); 168 169 String sMetadataPath = sTempDir + "/Metadata.xml"; 170 File metadataFile = new File (sMetadataPath); 171 172 XMLPrettyPrint pp = new XMLPrettyPrint(); 173 pp.printNodeToFile(out.getDocumentElement(), metadataFile); 174 addToZip(zos, metadataFile.getPath()); 175 176 metadataFile.delete(); 177 } 178 zos.close(); 179 fos.close(); 180 if(bShowContent || bShowMetadata){ 181 File zipFile = new File (sZipPath); 182 mailFile(sEmail, zipFile); 183 } 184 185 } catch (Exception e) { 186 throw new RemoteException (e.getLocalizedMessage()); 187 } 188 } 189 private void recurseDir(ZipOutputStream zos, String filePath) throws ConfigException, IOException { 190 File file = new File (filePath); 191 File [] fileList = file.listFiles(); 192 for (int i = 0; i < fileList.length; i++) { 193 if (fileList[i].isDirectory()) { 194 recurseDir (zos, fileList[i].getPath()); 195 } else if (fileList[i].isFile()) { 196 addToZip(zos, fileList[i].getPath()); 198 } 199 } 200 } 201 private void addToZip(ZipOutputStream zos, String filePath) throws IOException , ConfigException{ 202 FileInputStream in = new FileInputStream (filePath); 203 String sTempDir = ConfigSettings.getProperty(PNAME_UPLOADED_TEMPFILE_DIR); 204 205 String zipPath = filePath.substring(sTempDir.length()-1); 206 zos.putNextEntry(new ZipEntry (zipPath)); 207 byte[] buffer = new byte[18024]; 208 int len; 209 while((len = in.read(buffer)) > 0){ 210 zos.write(buffer, 0, len); 211 } 212 } 213 private void writeCollection(AbstractParentObject parent, File parentDir) throws ConfigException, DataAccessException, PublishException, StateException, RemoteException , SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 214 File dir = new File (parentDir.getAbsolutePath() + "/" + parent.getName()); 215 dir.mkdir(); 216 Iterator iter = parent.getChildren().iterator(); 217 while(iter.hasNext()){ 218 AbstractChildObject child = (AbstractChildObject) iter.next(); 219 220 if(child instanceof AbstractParentObject) { 221 AbstractParentObject sub = (AbstractParentObject) child; 222 writeCollection(sub, dir); 223 } else if(child.getStatus() == Status.APPROVED) { 224 writeDocument(child, getResourceTemplate().getDocumentElement(), dir); 225 } 226 } 227 228 } 229 private void writeDocument(AbstractChildObject resource,Element templateRoot, File parentDir) throws DataAccessException, PublishException, SAXException , IOException , ParserConfigurationException , FactoryConfigurationError , StateException, RemoteException { 230 File file = new File (parentDir.getAbsolutePath() + "/" + resource.getName() + ".xml"); 231 232 HarmoniseOutput out = new HarmoniseOutput(getDataStoreInterface()); 233 Element elResource = resource.publish(templateRoot,out,getState()); 234 235 XMLPrettyPrint pp = new XMLPrettyPrint(); 236 pp.printNodeToFile(elResource, file); 237 } 238 private void writePropertyGroup(PropertyGroup propGroup, Element topEl, HarmoniseOutput out) throws HarmoniseFactoryException, ConfigException, DataAccessException, PublishException, StateException, RemoteException , SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 239 Element docEl = propGroup.publish(getPropertyGroupTemplate().getDocumentElement(), out, getState()); 240 241 Iterator iter = propGroup.getChildren().iterator(); 242 while(iter.hasNext()){ 243 AbstractChildObject child = (AbstractChildObject) iter.next(); 244 245 if(child instanceof PropertyGroup) { 246 PropertyGroup sub = (PropertyGroup) child; 247 writePropertyGroup(sub, topEl, out); 248 } else if(child.getStatus() == Status.APPROVED) { 249 writeProperty(child, getPropertyTemplate().getDocumentElement(), docEl, out); 250 } 251 } 252 topEl.appendChild(docEl); 253 } 254 private void writeProperty(AbstractChildObject prop, Element templateRoot, Element topEl, HarmoniseOutput out) throws PublishException, StateException, RemoteException , SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 255 Element docEl = prop.publish(getPropertyTemplate().getDocumentElement(), out, getState()); 256 topEl.appendChild(docEl); 257 } 258 private AbstractDataStoreInterface getDataStoreInterface() 259 throws RemoteException { 260 if (m_dsi == null) { 261 try { 262 m_dsi = DataStoreInterfaceFactory.getDataStoreInterface(); 263 } catch (DataStoreException e) { 264 throw new RemoteException (e.getLocalizedMessage(), e); 265 } 266 } 267 return m_dsi; 268 } 269 private void convertWebdavPaths(Document doc) 270 throws DOMException, NameResolverException { 271 NodeList pathNodes = 273 doc.getElementsByTagName(AbstractChildObject.TAG_PATH); 274 Element curElm = null; 275 String sPath = null; 276 for (int i = 0; i < pathNodes.getLength(); i++) { 278 curElm = (Element) pathNodes.item(i); Text pathText = (Text) curElm.getFirstChild(); 280 if (pathText != null) { 282 sPath = pathText.getData(); 283 if(sPath.startsWith(HarmoniseNameResolver.URL_SEPARATOR)) { 284 String sRealPath = 285 HarmoniseNameResolver.getRealPath(sPath); 286 287 if (sRealPath != null) { 288 pathText.setData(sRealPath); 289 } 290 } 291 } 292 } 293 NodeList xincludes = 295 doc.getElementsByTagNameNS( 296 NamespaceType.XINCLUDE.getURI(), 297 "include"); 298 String sHref = null; 299 for (int i = 0; i < xincludes.getLength(); i++) { 300 curElm = (Element) xincludes.item(i); 301 Attr hrefAttr = 303 (Attr) curElm.getAttributeNode( 304 ATTRIB_HREF); 305 sHref = hrefAttr.getValue(); 307 if(sHref.startsWith(HarmoniseNameResolver.URL_SEPARATOR + HarmoniseNameResolver.WEBDAV_ROOT_MARKER)){ 308 hrefAttr.setValue(HarmoniseNameResolver.getRealPath(sHref)); 309 } 310 } 311 } 312 private void mailFile(String recipient_address, File file) throws ConfigException, Exception { 313 String sender_address = "HarmoniseServer"; 314 String message = "Contents of the Harmonise Server"; 315 String subject = "Harmonise Content"; 316 Email email = new Email(ConfigSettings.getProperty(PNAME_EMAIL_HOST),recipient_address, sender_address, 317 subject, message, file); 318 if(email.send() == false ){ 319 throw new RemoteException ("There was a problem emailing the file " + file.getPath()); 320 } else { 321 file.delete(); 322 } 323 } 324 public static void main(String args[]){ 325 String sReturn = ""; 326 try { 327 PublishService service = new PublishService(); 328 String sXml = null; 329 330 sXml = "<HarmonisePublishService><State>" + 331 "<Page id=\"1000\"/>" + 332 "</State></HarmonisePublishService>"; 333 sReturn = service.publish(sXml); 334 } catch (RemoteException e) { 335 System.out.println(e.getLocalizedMessage()); 337 } 338 System.out.println(sReturn); 339 System.exit(0); 340 } 341 private Document getResourceTemplate() throws SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 342 String sXml = "<HarmoniseObject><Name/><DisplayName/><Summary/><Content/><Profile default='true'><AllPropertyInstances/></Profile><Status/></HarmoniseObject>"; 343 return XMLDocument.getXMLDocumentFromString(sXml); 344 } 345 private Document getPropertyGroupTemplate() throws SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 346 String sXml = "<PropertyGroup><Name/><Summary/></PropertyGroup>"; 347 return XMLDocument.getXMLDocumentFromString(sXml); 348 } 349 private Document getPropertyTemplate() throws SAXException , IOException , ParserConfigurationException , FactoryConfigurationError { 350 String sXml = "<Property><DomainList/><Range><RangeDetails/><RangeObject/></Range></Property>"; 351 return XMLDocument.getXMLDocumentFromString(sXml); 352 } 353 private State getState() throws StateException, RemoteException { 354 State state = new State(getDataStoreInterface()); 355 return state; 356 } 357 } 358 | Popular Tags |