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