1 13 package info.magnolia.cms.exchange.simple; 14 15 import info.magnolia.cms.core.Content; 16 import info.magnolia.cms.core.HierarchyManager; 17 import info.magnolia.cms.core.ItemType; 18 import info.magnolia.cms.core.MetaData; 19 import info.magnolia.cms.core.NodeData; 20 import info.magnolia.cms.security.AccessDeniedException; 21 import info.magnolia.cms.security.Authenticator; 22 23 import java.io.IOException ; 24 import java.net.URL ; 25 import java.net.URLConnection ; 26 import java.util.Collection ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import javax.jcr.PathNotFoundException; 31 import javax.jcr.PropertyType; 32 import javax.jcr.RepositoryException; 33 import javax.servlet.http.HttpServletRequest ; 34 35 import org.apache.commons.lang.StringUtils; 36 import org.apache.log4j.Logger; 37 38 39 44 public class ContentWriter { 45 46 private static Logger log = Logger.getLogger(ContentWriter.class); 47 48 private String repositoryName; 49 50 private String baseURL; 51 52 private String credentials; 53 54 private HttpServletRequest request; 55 56 private HierarchyManager hierarchyManager; 57 58 public ContentWriter() { 59 } 60 61 public ContentWriter(HierarchyManager hm, String repositoryName, String baseURL, HttpServletRequest request) { 62 this.repositoryName = repositoryName; 63 this.baseURL = baseURL; 64 this.request = request; 65 this.credentials = Authenticator.getCredentials(this.request); 66 this.hierarchyManager = hm; 67 } 68 69 public void writeObject(String destination, Object content) throws RepositoryException { 70 if (content instanceof SerializableContentNode) { 71 this.writeObject(this.hierarchyManager.getContent(destination), (SerializableContentNode) content); 72 } 73 else if (content instanceof SerializableContent) { 74 this.writeObject(this.hierarchyManager.getContent(destination), (SerializableContent) content); 75 } 76 } 77 78 public void writeObject(Content destination, SerializableContent content) throws RepositoryException { 79 this.writeContent(destination, content); 80 this.hierarchyManager.save(); 81 if (log.isDebugEnabled()) { 82 log.debug("Path " + destination.getHandle() + " saved"); } 84 } 85 86 public void writeObject(Content destination, SerializableContentNode contentNode) throws RepositoryException { 87 this.writeContent(destination, contentNode); 88 this.hierarchyManager.save(); 89 if (log.isDebugEnabled()) { 90 log.debug("Path " + destination.getHandle() + " saved"); } 92 } 93 94 98 private void writeContent(Content parent, SerializableContent serializableContent) throws RepositoryException { 99 String newPageName = serializableContent.getName(); 100 String parentHandle = parent.getHandle(); 101 if (parentHandle.equals("/")) { parentHandle = StringUtils.EMPTY; 103 } 104 Content content = null; 105 if (serializableContent instanceof SerializableContentNode) { 106 try { 107 content = this.hierarchyManager.getContent(parentHandle + "/" + newPageName); this.safeDelete(content); 109 } 110 catch (PathNotFoundException e) { 111 content = this.hierarchyManager.createContent(parent.getHandle(), newPageName, ItemType.CONTENTNODE 112 .getSystemName()); 113 } 114 } 115 else { 116 try { 117 content = this.hierarchyManager.getContent(parentHandle + "/" + newPageName); this.safeDelete(content); 119 } 120 catch (PathNotFoundException e) { 121 content = this.hierarchyManager.createPage(parent.getHandle(), newPageName); 122 } 123 } 124 try { 125 126 this.writeMetaData(content, serializableContent.getMetaData()); 127 128 this.writeNodeData(content, serializableContent); 129 } 130 catch (Exception e) { 131 log.error(e.getMessage(), e); 132 } 133 134 this.writeContentNode(content, serializableContent); 135 Iterator contentIterator = serializableContent.getContentCollection().iterator(); 136 while (contentIterator.hasNext()) { 137 SerializableContent sContent = (SerializableContent) contentIterator.next(); 138 try { 139 this.writeContent(content, sContent); 140 } 141 catch (RepositoryException re) { 142 log.error("Failed to update " + (parent.getHandle() + "/" + newPageName)); log.error(re.getMessage(), re); 144 } 145 } 146 } 147 148 private void safeDelete(Content content) { 149 if (log.isDebugEnabled()) { 152 log.debug("Removing existing page " + content.getHandle()); } 154 this.removeNodedataList(content); 155 this.removeContentNodeList(content); 156 } 157 158 private void removeNodedataList(Content node) { 160 Collection subNodes = node.getNodeDataCollection(); 161 if (subNodes.size() > 0) { 162 Iterator nodeIterator = subNodes.iterator(); 163 while (nodeIterator.hasNext()) { 164 NodeData subNode = (NodeData) nodeIterator.next(); 165 try { 166 node.deleteNodeData(subNode.getName()); 167 } 168 catch (RepositoryException re) { 169 log.error("Failed to remove node data - " + subNode.getHandle()); log.error(re.getMessage(), re); 171 } 172 } 173 } 174 } 175 176 private void removeContentNodeList(Content node) { 177 Collection subNodes = node.getChildren(ItemType.CONTENTNODE); 178 if (subNodes.size() > 0) { 179 Iterator nodeIterator = subNodes.iterator(); 180 while (nodeIterator.hasNext()) { 181 Content subNode = (Content) nodeIterator.next(); 182 try { 183 node.delete(subNode.getName()); 184 } 185 catch (RepositoryException re) { 186 log.error("Failed to remove content node - " + subNode.getHandle()); log.error(re.getMessage(), re); 188 } 189 } 190 } 191 } 192 193 197 private void writeContentNode(Content parent, SerializableContent serializableContent) { 198 Iterator contentNodeIterator = serializableContent.getContentNodeCollection().iterator(); 199 while (contentNodeIterator.hasNext()) { 200 SerializableContentNode sContentNode = (SerializableContentNode) contentNodeIterator.next(); 201 try { 202 Content newContentNode = parent.createContent(sContentNode.getName(), ItemType.CONTENTNODE); 203 try { 204 205 this.writeMetaData(newContentNode, sContentNode.getMetaData()); 206 this.writeNodeData(newContentNode, sContentNode); 207 } 208 catch (Exception e) { 209 log.error(e.getMessage(), e); 210 } 211 212 if (sContentNode.getContentNodeCollection().size() > 0) { 213 this.writeContentNode(newContentNode, sContentNode); 214 } 215 } 216 catch (RepositoryException re) { 217 log.error(re.getMessage(), re); 218 } 219 } 220 } 221 222 private void writeMetaData(Content content, SerializableMetaData serializableMetaData) throws AccessDeniedException { 223 if (serializableMetaData == null) { 224 return; 225 } 226 MetaData metaData = content.getMetaData(); 227 List propertyList = serializableMetaData.getMetaProperties(); 228 for (int index = 0; index < propertyList.size(); index++) { 229 MetaDataProperty property = (MetaDataProperty) propertyList.get(index); 230 switch (property.getType()) { 231 case PropertyType.STRING: 232 metaData.setProperty(property.getName(), property.getString()); 233 break; 234 case PropertyType.LONG: 235 metaData.setProperty(property.getName(), property.getLong()); 236 break; 237 case PropertyType.DOUBLE: 238 metaData.setProperty(property.getName(), property.getDouble()); 239 break; 240 case PropertyType.BOOLEAN: 241 metaData.setProperty(property.getName(), property.getBoolean()); 242 break; 243 case PropertyType.DATE: 244 metaData.setProperty(property.getName(), property.getDate()); 245 break; 246 case PropertyType.NAME: 247 log.debug("Ignoring NAME property"); break; 250 default: 251 log.error("Unknown property type - " + property.getType()); } 253 } 254 } 255 256 260 private void writeNodeData(Content content, SerializableContent serializableContent) throws Exception { 261 Iterator nodeDataIterator = serializableContent.getNodeDataCollection().iterator(); 262 while (nodeDataIterator.hasNext()) { 263 SerializableNodeData sNodeData = (SerializableNodeData) nodeDataIterator.next(); 264 if (log.isDebugEnabled()) { 265 log.debug("Writing NodeData list for " + content.getHandle()); log.debug("Writing NodeData [ " + sNodeData.getName() + " ] Type [ " + PropertyType.nameFromValue(sNodeData.getType()) + " ]"); } 270 try { 271 NodeData nodeData = content.createNodeData(sNodeData.getName()); 272 switch (sNodeData.getType()) { 273 case PropertyType.STRING: 274 nodeData.setValue(sNodeData.getString()); 275 break; 276 case PropertyType.LONG: 277 nodeData.setValue(sNodeData.getLong()); 278 break; 279 case PropertyType.DOUBLE: 280 nodeData.setValue(sNodeData.getDouble()); 281 break; 282 case PropertyType.BOOLEAN: 283 nodeData.setValue(sNodeData.getBoolean()); 284 break; 285 case PropertyType.DATE: 286 nodeData.setValue(sNodeData.getDate()); 287 break; 288 case PropertyType.BINARY: 289 String binaryResourceHandle = sNodeData.getBinaryAsLink(); 290 URL url = new URL (this.baseURL); 291 URLConnection urlConnection = url.openConnection(); 292 urlConnection.setRequestProperty("Authorization", credentials); urlConnection.addRequestProperty(Syndicator.PAGE, binaryResourceHandle); 294 urlConnection.addRequestProperty(Syndicator.ACTION, Syndicator.GET); 295 urlConnection.addRequestProperty(Syndicator.GET_TYPE, Syndicator.GET_TYPE_BINARY); 296 urlConnection.addRequestProperty(Syndicator.WORKING_CONTEXT, this.repositoryName); 297 nodeData.setValue(urlConnection.getInputStream()); 298 } 299 } 300 catch (RepositoryException re) { 301 log.error(re.getMessage(), re); 302 } 303 catch (IOException e) { 304 log.error(e.getMessage(), e); 305 } 306 } 307 } 308 } 309
| Popular Tags
|