1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.admininterface;
35
36 import info.magnolia.cms.beans.config.URI2RepositoryManager;
37 import info.magnolia.cms.beans.config.URI2RepositoryMapping;
38 import info.magnolia.cms.beans.runtime.Document;
39 import info.magnolia.cms.beans.runtime.FileProperties;
40 import info.magnolia.cms.beans.runtime.MultipartForm;
41 import info.magnolia.cms.core.Content;
42 import info.magnolia.cms.core.HierarchyManager;
43 import info.magnolia.cms.core.ItemType;
44 import info.magnolia.cms.core.MgnlNodeType;
45 import info.magnolia.cms.core.NodeData;
46 import info.magnolia.cms.core.Path;
47 import info.magnolia.cms.gui.control.ControlImpl;
48 import info.magnolia.cms.gui.control.File;
49 import info.magnolia.cms.gui.fckeditor.FCKEditorTmpFiles;
50 import info.magnolia.cms.security.AccessDeniedException;
51 import info.magnolia.cms.security.SecurityUtil;
52 import info.magnolia.cms.util.ContentUtil;
53 import info.magnolia.cms.util.DateUtil;
54 import info.magnolia.cms.util.ExclusiveWrite;
55 import info.magnolia.cms.util.NodeDataUtil;
56 import info.magnolia.context.MgnlContext;
57 import info.magnolia.link.LinkUtil;
58 import info.magnolia.objectfactory.Classes;
59 import info.magnolia.objectfactory.Components;
60 import info.magnolia.objectfactory.MgnlInstantiationException;
61 import info.magnolia.repository.RepositoryConstants;
62
63 import java.io.FileInputStream;
64 import java.io.FileNotFoundException;
65 import java.io.IOException;
66 import java.io.InputStream;
67 import java.util.ArrayList;
68 import java.util.Calendar;
69 import java.util.Collection;
70 import java.util.GregorianCalendar;
71 import java.util.Iterator;
72 import java.util.List;
73 import java.util.Map;
74 import java.util.TimeZone;
75 import java.util.regex.Matcher;
76 import java.util.regex.Pattern;
77
78 import javax.jcr.Node;
79 import javax.jcr.PathNotFoundException;
80 import javax.jcr.PropertyType;
81 import javax.jcr.RepositoryException;
82 import javax.jcr.Value;
83 import javax.jcr.ValueFactory;
84
85 import org.apache.commons.codec.binary.Base64;
86 import org.apache.commons.io.FileUtils;
87 import org.apache.commons.io.IOUtils;
88 import org.apache.commons.lang.StringEscapeUtils;
89 import org.apache.commons.lang.StringUtils;
90 import org.devlib.schmidt.imageinfo.ImageInfo;
91 import org.slf4j.Logger;
92 import org.slf4j.LoggerFactory;
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 public class SaveHandlerImpl implements SaveHandler {
132
133
134
135
136 public static Logger log = LoggerFactory.getLogger(SaveHandlerImpl.class);
137
138
139
140
141 private MultipartForm form;
142
143
144
145
146 private boolean create;
147
148 private ItemType creationItemType = ItemType.CONTENTNODE;
149 private ItemType collectionNodeCreationItemType = ItemType.CONTENTNODE;
150
151
152
153
154 private String repository = RepositoryConstants.WEBSITE;
155
156
157
158
159 private String path;
160
161
162
163
164 private String nodeCollectionName;
165
166
167
168
169 private String nodeName;
170
171
172
173
174 private String paragraph;
175
176
177
178
179 public SaveHandlerImpl() {
180 }
181
182
183
184
185
186 @Override
187 public void init(MultipartForm form) {
188 this.setForm(form);
189 }
190
191
192
193
194 @Override
195 public boolean save() {
196
197 String[] saveInfos = getForm().getParameterValues("mgnlSaveInfo");
198
199 if (saveInfos == null) {
200 log.info("Nothing to save, mgnlSaveInfo parameter not found.");
201 return true;
202 }
203
204 synchronized (ExclusiveWrite.getInstance()) {
205
206 String path = this.getPath();
207
208 HierarchyManager hm = MgnlContext.getHierarchyManager(this.getRepository());
209 try {
210
211 Content page = this.getPageNode(hm);
212
213 if (page == null) {
214
215 return false;
216 }
217
218 Content node = this.getSaveNode(hm, page);
219
220
221 this.setNodeName(node.getName());
222 if (StringUtils.isEmpty(node.getMetaData().getTemplate())) {
223 node.getMetaData().setTemplate(this.getParagraph());
224 }
225
226
227 for (int i = 0; i < saveInfos.length; i++) {
228 String saveInfo = saveInfos[i];
229 processSaveInfo(node, saveInfo);
230 }
231
232
233 try {
234 MultipartForm form = getForm();
235 Map docs = form.getDocuments();
236 Iterator iter = docs.keySet().iterator();
237 while (iter.hasNext()) {
238 form.getDocument((String) iter.next()).delete();
239 }
240 }
241 catch (Exception e) {
242 log.error("Could not delete temp documents from form");
243 }
244
245 log.debug("Saving {}", path);
246
247
248 updateMetaData(node);
249
250
251
252 if (this.getRepository().equals(RepositoryConstants.WEBSITE)) {
253 while (node.getItemType().equals(ItemType.CONTENTNODE)) {
254 node = node.getParent();
255 node.updateMetaData();
256 }
257 }
258
259 hm.save();
260 }
261 catch (RepositoryException re) {
262 log.error(re.getMessage(), re);
263 return false;
264 }
265 }
266 return true;
267 }
268
269
270
271
272
273
274 private void updateMetaData(Content currentNode) throws AccessDeniedException, RepositoryException {
275 if(currentNode.isNodeType(MgnlNodeType.NT_FOLDER)) {
276 return;
277 }
278
279 currentNode.updateMetaData();
280
281 if(!RepositoryConstants.WEBSITE.equals(currentNode.getWorkspace().getName()) || currentNode.isNodeType(MgnlNodeType.NT_CONTENT) || currentNode.isNodeType(MgnlNodeType.NT_FOLDER) || currentNode.getLevel()<2) {
282 return;
283 } else {
284 updateMetaData(currentNode.getParent());
285 }
286 }
287
288
289
290
291
292
293
294
295
296 protected void processSaveInfo(Content node, String saveInfo) throws PathNotFoundException, RepositoryException,
297 AccessDeniedException {
298
299 String name;
300 int type = PropertyType.STRING;
301 int valueType = ControlImpl.VALUETYPE_SINGLE;
302 int isRichEditValue = 0;
303 int encoding = ControlImpl.ENCRYPTION_NO;
304 String[] values = {StringUtils.EMPTY};
305 if (StringUtils.contains(saveInfo, ',')) {
306 String[] info = StringUtils.split(saveInfo, ',');
307 name = info[0];
308 if (info.length >= 2) {
309 type = PropertyType.valueFromName(info[1]);
310 }
311 if (info.length >= 3) {
312 valueType = Integer.valueOf(info[2]).intValue();
313 }
314 if (info.length >= 4) {
315 isRichEditValue = Integer.valueOf(info[3]).intValue();
316 }
317 if (info.length >= 5) {
318 encoding = Integer.valueOf(info[4]).intValue();
319 }
320 }
321 else {
322 name = saveInfo;
323 }
324
325
326 String customSaveHandler = this.getForm().getParameter(name + "_saveHandler");
327 if (!StringUtils.isEmpty(customSaveHandler)) {
328 try {
329 Class cshClazz = Classes.getClassFactory().forName(customSaveHandler);
330 if (!FieldSaveHandler.class.isAssignableFrom(cshClazz)) {
331 log.error("Class {} must implement the FieldSaveHandler interface", cshClazz);
332 throw new ClassCastException("Class " + cshClazz + " must implement the FieldSaveHandler interface");
333 }
334 else {
335 FieldSaveHandler csh = (FieldSaveHandler)Components.newInstance(cshClazz);
336 HierarchyManager hm = MgnlContext.getSystemContext().getHierarchyManager(RepositoryConstants.CONFIG);
337
338 String configPath = this.getForm().getParameter(name + "_configNode");
339 Content configNode = null;
340 if (StringUtils.isNotEmpty(configPath))
341 {
342 configNode = hm.getContent(configPath);
343 }
344
345 csh.save(
346 node,
347 configNode,
348 name,
349 getForm(),
350 type,
351 valueType,
352 isRichEditValue,
353 encoding);
354 }
355 }
356 catch (ClassNotFoundException e) {
357 log.error("Error loading class " + customSaveHandler, e);
358 }
359 catch (MgnlInstantiationException e) {
360 log.error("Error creating instance of class " + customSaveHandler, e);
361 }
362 }
363 else
364
365
366 if (type == PropertyType.BINARY) {
367 processBinary(node, name);
368 }
369 else {
370 values = getForm().getParameterValues(name);
371 if (valueType == ControlImpl.VALUETYPE_MULTIPLE) {
372 processMultiple(node, name, type, values);
373 }
374 else if (isRichEditValue != ControlImpl.RICHEDIT_NONE) {
375 processRichEdit(node, name, type, isRichEditValue, encoding, values);
376 }
377 else if (type == PropertyType.DATE) {
378 processDate(node, name, type, valueType, encoding, values);
379 }
380 else {
381 processCommon(node, name, type, valueType, encoding, values);
382 }
383 }
384 }
385
386 protected void processDate(Content node, String name, int type, int valueType, int encoding, String[] values) {
387 try {
388 if (StringUtils.isEmpty(values[0])) {
389 log.debug("Date has no value. Deleting node data {}", name);
390 if (node.hasNodeData(name)) {
391 node.deleteNodeData(name);
392 }
393 }
394 else {
395 Calendar utc = DateUtil.getUTCCalendarFromDialogString(values[0]);
396 NodeDataUtil.getOrCreate(node, name).setValue(utc);
397 }
398 }
399 catch (Exception e) {
400 log.error("Could not update date value of node:" + node.getHandle() + " of property:" + name, e);
401 }
402 }
403
404
405
406
407
408
409
410
411
412
413
414
415
416 protected void processRichEdit(Content node, String name, int type, int isRichEditValue, int encoding,
417 String[] values) throws PathNotFoundException, RepositoryException, AccessDeniedException {
418 String valueStr = StringUtils.EMPTY;
419 if (values != null) {
420 valueStr = values[0];
421 }
422
423 valueStr = cleanLineBreaks(valueStr, isRichEditValue);
424
425 if (isRichEditValue == ControlImpl.RICHEDIT_FCK) {
426
427 valueStr = valueStr.replaceAll("<span scaytid=\"\\d*\" scayt_word=\".*?\">(.*?)</span>", "$1");
428 valueStr = updateLinks(node, name, valueStr);
429 }
430
431 processString(node, name, type, encoding, values, valueStr);
432 }
433
434
435
436
437
438
439
440
441
442 protected String cleanLineBreaks(String valueStr, int isRichEditValue) {
443 valueStr = StringUtils.replace(valueStr, "\r\n", " ");
444 valueStr = StringUtils.replace(valueStr, "\n", " ");
445
446
447 valueStr = StringUtils.replace(valueStr, "</br>", StringUtils.EMPTY);
448 valueStr = StringUtils.replace(valueStr, "<br>", "<br/>");
449 valueStr = StringUtils.replace(valueStr, "<BR>", "<br />");
450 valueStr = StringUtils.replace(valueStr, "<br/>", "<br />");
451 valueStr = StringUtils.replace(valueStr, "<P><br />", "<P>");
452
453
454 if (isRichEditValue != ControlImpl.RICHEDIT_FCK) {
455 valueStr = replacePByBr(valueStr, "p");
456 }
457 return valueStr;
458 }
459
460
461
462
463
464
465
466
467
468
469
470
471 protected String updateLinks(Content node, String name, String valueStr) throws AccessDeniedException,
472 RepositoryException, PathNotFoundException {
473
474
475 HierarchyManager hm = MgnlContext.getHierarchyManager(this.getRepository());
476
477 Pattern imageOrDowloadPattern = Pattern.compile("(<(a|img|embed)[^>]+(href|src)[ ]*=[ ]*\")([^\"]*)(\"[^>]*>)");
478 Pattern tmpFilePattern = Pattern.compile("/tmp/fckeditor/([^/]*)/[^\"]*");
479
480 Content filesNode = ContentUtil.getOrCreateContent(node, name + "_files", ItemType.CONTENTNODE);
481
482
483 List usedFiles = new ArrayList();
484
485
486 Matcher imageOrDowloadMatcher = imageOrDowloadPattern.matcher(valueStr);
487 StringBuffer res = new StringBuffer();
488 while (imageOrDowloadMatcher.find()) {
489 String src = imageOrDowloadMatcher.group(4);
490
491 String link = StringUtils.removeStart(src, MgnlContext.getContextPath());
492 link = StringEscapeUtils.unescapeHtml(link);
493
494
495 Matcher tmpFileMatcher = tmpFilePattern.matcher(src);
496
497 if (tmpFileMatcher.find()) {
498 String uuid = tmpFileMatcher.group(1);
499
500 Document doc = FCKEditorTmpFiles.getDocument(uuid);
501 String fileNodeName = Path.getUniqueLabel(hm, filesNode.getHandle(), "file");
502 Content fileNode = ContentUtil.getOrCreateContent(filesNode, fileNodeName, ItemType.CONTENTNODE);
503 SaveHandlerImpl.saveDocument(fileNode, doc, "document", "", "");
504 link = filesNode.getHandle() + "/" + fileNodeName + "/document/" + doc.getFileNameWithExtension();
505 doc.delete();
506 try {
507 FileUtils.deleteDirectory(new java.io.File(Path.getTempDirectory() + "/fckeditor/" + uuid));
508 }
509 catch (IOException e) {
510 log.error("can't delete tmp file [" + Path.getTempDirectory() + "/fckeditor/" + uuid + "]");
511 }
512 }
513
514 String prefixRepo = "";
515 String mappedUri = "";
516 URI2RepositoryManager uri2RepositoryManager = URI2RepositoryManager.getInstance();
517 if (this.repository.equals(uri2RepositoryManager.getDefaultMapping().getRepository())) {
518 prefixRepo = uri2RepositoryManager.getDefaultMapping().getURIPrefix();
519 } else {
520 Collection<URI2RepositoryMapping> mappings = uri2RepositoryManager.getMappings();
521 for (URI2RepositoryMapping mapping : mappings) {
522 if (mapping.getRepository().equals(this.repository)) {
523 prefixRepo = mapping.getURIPrefix();
524 if(!StringUtils.isEmpty(mapping.getHandlePrefix()) && link.startsWith(prefixRepo)){
525 mappedUri = mapping.getURI(filesNode.getHandle());
526 mappedUri = StringUtils.substringBeforeLast(mappedUri, ".");
527 }
528 break;
529 }
530 }
531 }
532
533 prefixRepo = StringUtils.removeEnd(prefixRepo, "/");
534
535 link = cleanupLinkPrefix(filesNode, link, prefixRepo);
536
537
538 link = StringUtils.replace(link, "$", "\\$");
539
540 imageOrDowloadMatcher.appendReplacement(res, "$1" + link + "$5");
541
542 if (link.startsWith(prefixRepo + filesNode.getHandle())) {
543 String fileNodeName = StringUtils.removeStart(link, prefixRepo + filesNode.getHandle() + "/");
544 fileNodeName = StringUtils.substringBefore(fileNodeName, "/");
545 usedFiles.add(fileNodeName);
546 }else if (!StringUtils.isEmpty(mappedUri) && link.startsWith(mappedUri)){
547 String fileNodeName = StringUtils.removeStart(link, mappedUri + "/");
548 fileNodeName = StringUtils.substringBefore(fileNodeName, "/");
549 usedFiles.add(fileNodeName);
550 }
551 }
552
553
554 for (Iterator iter = filesNode.getChildren().iterator(); iter.hasNext();) {
555 Content fileNodeData = (Content) iter.next();
556 if (!usedFiles.contains(fileNodeData.getName())) {
557 fileNodeData.delete();
558 }
559 }
560
561 imageOrDowloadMatcher.appendTail(res);
562 valueStr = res.toString();
563
564
565 valueStr = LinkUtil.convertAbsoluteLinksToUUIDs(valueStr);
566
567 return valueStr;
568 }
569
570 protected String cleanupLinkPrefix(Content filesNode, String link, String prefixRepo) {
571 if (link.contains(filesNode.getHandle()) && !link.startsWith(prefixRepo + "/")) {
572 link = prefixRepo + link;
573 }
574 return link;
575 }
576
577
578
579
580
581
582
583
584
585
586
587
588
589 protected void processCommon(Content node, String name, int type, int valueType, int encoding, String[] values)
590 throws PathNotFoundException, RepositoryException, AccessDeniedException {
591 String valueStr = StringUtils.EMPTY;
592 if (values != null) {
593 valueStr = values[0];
594 }
595
596 processString(node, name, type, encoding, values, valueStr);
597 }
598
599
600
601
602
603
604
605
606
607
608
609
610
611 protected void processString(Content node, String name, int type, int encoding, String[] values, String valueStr)
612 throws PathNotFoundException, RepositoryException, AccessDeniedException {
613
614 boolean remove = false;
615 boolean write = false;
616 if (encoding == ControlImpl.ENCRYPTION_NO_ENCODING_BASE64) {
617 if (StringUtils.isNotBlank(valueStr)) {
618 valueStr = new String(Base64.encodeBase64(valueStr.getBytes()));
619 write = true;
620 }
621 } else if (encoding == ControlImpl.ENCRYPTION_HASH_SHA) {
622 if (StringUtils.isNotEmpty(valueStr)) {
623 valueStr = SecurityUtil.getSHA1Hex(valueStr);
624 write = true;
625 }
626 } else if (encoding == ControlImpl.ENCRYPTION_HASH_BCRYPT) {
627 if (StringUtils.isNotEmpty(valueStr)) {
628 valueStr = SecurityUtil.getBCrypt(valueStr);
629 write = true;
630 }
631 } else {
632
633 if (values == null || StringUtils.isEmpty(valueStr)) {
634 remove = true;
635 } else {
636 write = true;
637 }
638 }
639 if (remove) {
640 processRemoveCommon(node, name);
641 }
642 else if (write) {
643 processWriteCommon(node, name, valueStr, type);
644 }
645 }
646
647
648
649
650 protected void processRemoveCommon(Content node, String name) throws PathNotFoundException, RepositoryException {
651 NodeData data = node.getNodeData(name);
652
653 if (data.isExist()) {
654 node.deleteNodeData(name);
655 }
656 }
657
658
659
660
661
662
663
664
665
666 protected void processWriteCommon(Content node, String name, String valueStr, int type)
667 throws AccessDeniedException, RepositoryException {
668 Value value = this.getValue(valueStr, type);
669 if (null != value) {
670 NodeData data = NodeDataUtil.getOrCreate(node, name);
671 data.setValue(value);
672 }
673 }
674
675
676
677
678
679
680
681
682
683
684
685 protected void processMultiple(Content node, String name, int type, String[] values) throws RepositoryException,
686 PathNotFoundException, AccessDeniedException {
687
688 try {
689 node.delete(name);
690 }
691 catch (PathNotFoundException e) {
692 if (log.isDebugEnabled()) {
693 log.debug("Exception caught: " + e.getMessage(), e);
694 }
695 }
696 if (values != null && values.length != 0) {
697 Content multiNode = node.createContent(name, ItemType.CONTENTNODE);
698 try {
699
700 multiNode.deleteNodeData("creationdate");
701 }
702 catch (RepositoryException re) {
703 if (log.isDebugEnabled()) {
704 log.debug("Exception caught: " + re.getMessage(), re);
705 }
706 }
707 for (int j = 0; j < values.length; j++) {
708 String valueStr = values[j];
709 if (StringUtils.isNotEmpty(valueStr)) {
710 Value value = this.getValue(valueStr, type);
711 multiNode.createNodeData(Integer.toString(j)).setValue(value);
712 }
713 }
714 }
715 }
716
717
718
719
720
721
722
723
724
725 protected void processBinary(Content node, String name) throws PathNotFoundException, RepositoryException,
726 AccessDeniedException {
727 Document doc = getForm().getDocument(name);
728 if (doc == null && getForm().getParameter(name + "_" + File.REMOVE) != null) {
729 try {
730 node.deleteNodeData(name);
731 }
732 catch (RepositoryException re) {
733 if (log.isDebugEnabled()) {
734 log.debug("Exception caught: " + re.getMessage(), re);
735 }
736 }
737 }
738 else {
739 String fileName = getForm().getParameter(name + "_" + FileProperties.PROPERTY_FILENAME);
740 String template = getForm().getParameter(name + "_" + FileProperties.PROPERTY_TEMPLATE);
741
742 SaveHandlerImpl.saveDocument(node, doc, name, fileName, template);
743 }
744 }
745
746
747
748
749
750
751 public Value getValue(String s) {
752 return this.getValue(s, PropertyType.STRING);
753 }
754
755
756
757
758
759
760 public Value getValue(long l) {
761 HierarchyManager hm = MgnlContext.getHierarchyManager(this.getRepository());
762 ValueFactory valueFactory;
763 try {
764 valueFactory = hm.getWorkspace().getSession().getValueFactory();
765 }
766 catch (RepositoryException e) {
767 throw new RuntimeException(e);
768 }
769 return valueFactory.createValue(l);
770 }
771
772
773
774
775
776
777
778 public Value getValue(String valueStr, int type) {
779
780 ValueFactory valueFactory = null;
781
782 HierarchyManager hm = MgnlContext.getHierarchyManager(this.getRepository());
783 try {
784 valueFactory = hm.getWorkspace().getSession().getValueFactory();
785 }
786 catch (RepositoryException e) {
787 throw new RuntimeException(e);
788 }
789
790 Value value = null;
791
792 if (type == PropertyType.REFERENCE) {
793 try {
794 Node referencedNode = hm.getWorkspace().getSession().getNodeByUUID(valueStr);
795
796 value = valueFactory.createValue(referencedNode);
797 }
798 catch (RepositoryException re) {
799 if (log.isDebugEnabled()) {
800 log.debug("Cannot retrieve the referenced node by UUID: " + valueStr, re);
801 }
802 }
803 }
804 else {
805 value = NodeDataUtil.createValue(valueStr, type, valueFactory);
806 }
807
808 return value;
809 }
810
811
812
813
814
815 protected static String replacePByBr(final String value, String tagName) {
816
817 if (StringUtils.isBlank(value)) {
818 return value;
819 }
820
821 String fixedValue = value;
822
823 String pre = "<" + tagName + ">";
824 String post = "</" + tagName + ">";
825
826
827 if (fixedValue.endsWith(post)) {
828 fixedValue = StringUtils.substringBeforeLast(fixedValue, post);
829 }
830
831 fixedValue = StringUtils.replace(fixedValue, pre + " " + post, "\n ");
832 fixedValue = StringUtils.replace(fixedValue, pre, StringUtils.EMPTY);
833 fixedValue = StringUtils.replace(fixedValue, post, "\n\n ");
834
835 if (!tagName.equals(tagName.toUpperCase())) {
836 fixedValue = replacePByBr(fixedValue, tagName.toUpperCase());
837 }
838 return fixedValue;
839 }
840
841
842
843
844 @Override
845 public boolean isCreate() {
846 return create;
847 }
848
849
850
851
852 @Override
853 public void setCreate(boolean create) {
854 this.create = create;
855 }
856
857
858
859
860 @Override
861 public ItemType getCreationItemType() {
862 return creationItemType;
863 }
864
865
866
867
868 @Override
869 public void setCreationItemType(ItemType creationItemType) {
870 this.creationItemType = creationItemType;
871 }
872
873 @Override
874 public ItemType getCollectionNodeCreationItemType() {
875 return collectionNodeCreationItemType;
876 }
877
878 @Override
879 public void setCollectionNodeCreationItemType(ItemType collectionNodeCreationItemType) {
880 this.collectionNodeCreationItemType = collectionNodeCreationItemType;
881 }
882
883
884
885
886 protected MultipartForm getForm() {
887 return form;
888 }
889
890
891
892
893
894 protected void setForm(MultipartForm form) {
895 this.form = form;
896 }
897
898
899
900
901
902 @Override
903 public void setRepository(String repository) {
904 this.repository = repository;
905 }
906
907
908
909
910
911 @Override
912 public String getRepository() {
913 return repository;
914 }
915
916
917
918
919
920
921
922
923
924 protected Content getPageNode(HierarchyManager hm) throws RepositoryException, AccessDeniedException,
925 PathNotFoundException {
926 Content page = null;
927 String path = this.getPath();
928 try {
929 page = hm.getContent(path);
930 }
931 catch (RepositoryException e) {
932 if (this.isCreate()) {
933 String parentPath = StringUtils.substringBeforeLast(path, "/");
934 String label = StringUtils.substringAfterLast(path, "/");
935 if (StringUtils.isEmpty(parentPath)) {
936 page = hm.getRoot();
937 }
938 else {
939 page = hm.getContent(parentPath);
940 }
941 page = page.createContent(label, this.getCreationItemType());
942 }
943 else {
944 log.error("Tried to save a not existing node with path {}. use create = true to force creation", path);
945 }
946 }
947
948 return page;
949 }
950
951
952
953
954
955
956
957
958
959
960 protected Content getSaveNode(HierarchyManager hm, Content rootNode) throws AccessDeniedException,
961 RepositoryException {
962 Content node = null;
963
964
965 Content nodeCollection = null;
966 if (StringUtils.isNotEmpty(this.getNodeCollectionName())) {
967 try {
968 nodeCollection = rootNode.getContent(this.getNodeCollectionName());
969 }
970 catch (RepositoryException re) {
971
972 nodeCollection = rootNode.createContent(this.getNodeCollectionName(), this.getCollectionNodeCreationItemType());
973 log.debug("Create - {}" + nodeCollection.getHandle());
974 }
975 }
976 else {
977 nodeCollection = rootNode;
978 }
979
980
981 if (StringUtils.isNotEmpty(this.getNodeName())) {
982 try {
983 node = nodeCollection.getContent(this.getNodeName());
984 }
985 catch (RepositoryException re) {
986
987 if (this.getNodeName().equals("mgnlNew")) {
988 this.setNodeName(Path.getUniqueLabel(hm, nodeCollection.getHandle(), "0"));
989 }
990 node = nodeCollection.createContent(this.getNodeName(), this.getCreationItemType());
991 }
992 }
993 else {
994 node = nodeCollection;
995 }
996 return node;
997 }
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010 public static void saveDocument(Content node, Document doc, String name, String fileName, String template)
1011 throws PathNotFoundException, RepositoryException, AccessDeniedException {
1012
1013 NodeData data = node.getNodeData(name);
1014 if (doc != null) {
1015 if (!data.isExist()) {
1016 data = node.createNodeData(name, PropertyType.BINARY);
1017
1018 log.debug("creating under - {}", node.getHandle());
1019 log.debug("creating node data for binary store - {}", name);
1020
1021 }
1022 data.setValue(doc.getStream());
1023 log.debug("Node data updated");
1024 }
1025 if (data != null) {
1026 if ( StringUtils.isEmpty(fileName) ) {
1027 fileName = (doc == null) ? null : doc.getFileName();
1028 }
1029
1030 if (StringUtils.isEmpty(fileName)) {
1031 fileName = node.getName();
1032 }
1033
1034 data.setAttribute(FileProperties.PROPERTY_FILENAME, fileName);
1035
1036 if (doc != null) {
1037 data.setAttribute(FileProperties.PROPERTY_CONTENTTYPE, doc.getType());
1038
1039 Calendar value = new GregorianCalendar(TimeZone.getDefault());
1040 data.setAttribute(FileProperties.PROPERTY_LASTMODIFIED, value);
1041
1042 data.setAttribute(FileProperties.PROPERTY_SIZE, Long.toString(doc.getLength()));
1043
1044 data.setAttribute(FileProperties.PROPERTY_EXTENSION, doc.getExtension());
1045
1046 data.setAttribute(FileProperties.PROPERTY_TEMPLATE, template);
1047
1048 InputStream raf = null;
1049 try {
1050 ImageInfo ii = new ImageInfo();
1051 raf = new FileInputStream(doc.getFile());
1052 ii.setInput(raf);
1053 if (ii.check()) {
1054 data.setAttribute(FileProperties.PROPERTY_WIDTH, Long.toString(ii.getWidth()));
1055 data.setAttribute(FileProperties.PROPERTY_HEIGHT, Long.toString(ii.getHeight()));
1056
1057 }
1058 }
1059 catch (FileNotFoundException e) {
1060 log.error("FileNotFoundException caught when parsing {}, image data will not be available", doc
1061 .getFile()
1062 .getAbsolutePath());
1063 }
1064 finally {
1065 IOUtils.closeQuietly(raf);
1066 }
1067
1068
1069
1070
1071
1072 }
1073 }
1074
1075 }
1076
1077
1078
1079
1080 @Override
1081 public String getNodeCollectionName() {
1082 return this.nodeCollectionName;
1083 }
1084
1085
1086
1087
1088 @Override
1089 public void setNodeCollectionName(String nodeCollectionName) {
1090 this.nodeCollectionName = nodeCollectionName;
1091 }
1092
1093
1094
1095
1096 @Override
1097 public String getNodeName() {
1098 return this.nodeName;
1099 }
1100
1101
1102
1103
1104 @Override
1105 public void setNodeName(String nodeName) {
1106 this.nodeName = nodeName;
1107 }
1108
1109
1110
1111
1112 @Override
1113 public String getParagraph() {
1114 return this.paragraph;
1115 }
1116
1117
1118
1119
1120 @Override
1121 public void setParagraph(String paragraph) {
1122 this.paragraph = paragraph;
1123 }
1124
1125
1126
1127
1128 @Override
1129 public String getPath() {
1130 return this.path;
1131 }
1132
1133
1134
1135
1136 @Override
1137 public void setPath(String path) {
1138 this.path = path;
1139 }
1140
1141 }