/
XMLResource.java
926 lines (824 loc) · 36.1 KB
/
XMLResource.java
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
/*
* Copyright 2007 -2016 Wyona
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.wyona.org/licenses/APACHE-LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wyona.yanel.impl.resources;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Source;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathFactory;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.wyona.commons.xml.XMLHelper;
import org.wyona.yanel.core.Path;
import org.wyona.yanel.core.Resource;
import org.wyona.yanel.core.api.attributes.AnnotatableV1;
import org.wyona.yanel.core.api.attributes.CommentableV1;
import org.wyona.yanel.core.api.attributes.CreatableV2;
import org.wyona.yanel.core.api.attributes.IntrospectableV1;
import org.wyona.yanel.core.api.attributes.ModifiableV2;
import org.wyona.yanel.core.api.attributes.TranslatableV1;
import org.wyona.yanel.core.api.attributes.VersionableV2;
import org.wyona.yanel.core.api.attributes.VersionableV3;
import org.wyona.yanel.core.api.attributes.ViewableV1;
import org.wyona.yanel.core.api.attributes.ViewableV2;
import org.wyona.yanel.core.api.attributes.WorkflowableV1;
import org.wyona.yanel.core.attributes.commentable.CommentManagerV1;
import org.wyona.yanel.core.attributes.translatable.TranslationException;
import org.wyona.yanel.core.attributes.translatable.TranslationManager;
import org.wyona.yanel.core.attributes.versionable.RevisionInformation;
import org.wyona.yanel.core.attributes.viewable.View;
import org.wyona.yanel.core.attributes.viewable.ViewDescriptor;
import org.wyona.yanel.core.source.HttpResolver;
import org.wyona.yanel.core.source.SourceException;
import org.wyona.yanel.core.source.SourceResolver;
import org.wyona.yanel.core.source.YanelStreamSource;
import org.wyona.yanel.core.util.ResourceAttributeHelper;
import org.wyona.yanel.core.workflow.Workflow;
import org.wyona.yanel.core.workflow.WorkflowException;
import org.wyona.yanel.core.workflow.WorkflowHelper;
import org.wyona.yarep.core.Node;
import org.wyona.yarep.core.NoSuchNodeException;
import org.wyona.yarep.core.Repository;
import org.wyona.yarep.core.Revision;
/**
* Generic resource handling XML data and transforming it using XSLT, etc.
*/
public class XMLResource extends BasicXMLResource implements ModifiableV2, VersionableV2, VersionableV3, CreatableV2, IntrospectableV1, TranslatableV1, WorkflowableV1, AnnotatableV1, CommentableV1 {
private static Logger log = LogManager.getLogger(XMLResource.class);
private Boolean annotationRead = false;
private Set<String> annotations = new HashSet<String>();
private static final String YANEL_PATH_PROPERTY_NAME = "yanel-path";
private static final String CONNECTION_TIMEOUT_PROPERTY_NAME = "connection-timeout";
private static final String SOCKET_TIMEOUT_PROPERTY_NAME = "socket-timeout";
/**
* @see org.wyona.yanel.core.api.attributes.ViewableV2#getView(java.lang.String)
*/
@Override
public View getView(String viewId) throws Exception {
return getView(viewId, null);
}
/**
* @see org.wyona.yanel.core.api.attributes.VersionableV2#getView(String, String)
*/
public View getView(String viewId, String revisionName) throws Exception {
InputStream xmlInputStream = getSourceXML(revisionName);
return getXMLView(viewId, xmlInputStream);
}
/**
* Get initial content as XML
* @param revisionName Name of revision for which source XML will be retrieved / generated
* @return TODO
*/
private InputStream getSourceXML(String revisionName) throws Exception {
String sourcePath = getCustomSourcePath();
if (sourcePath != null) {
if (log.isDebugEnabled()) log.debug("Source path: " + sourcePath);
if (sourcePath.startsWith("yanelrepo:") || sourcePath.startsWith("yanelresource:") || sourcePath.startsWith("http:") || sourcePath.startsWith("https:")) {
log.debug("Protocol/Scheme used: " + sourcePath);
// TODO: URL Re-writing (see for example http://j2ep.sourceforge.net/docs/rewrite.html)
try {
javax.xml.transform.URIResolver resolver = null;
if (sourcePath.startsWith("http:") || sourcePath.startsWith("https:")) {
if (isTimeoutConfigured(CONNECTION_TIMEOUT_PROPERTY_NAME) || isTimeoutConfigured(SOCKET_TIMEOUT_PROPERTY_NAME)) {
resolver = new HttpResolver(this, getTimeoutValue(CONNECTION_TIMEOUT_PROPERTY_NAME), getTimeoutValue(SOCKET_TIMEOUT_PROPERTY_NAME));
} else {
resolver = new HttpResolver(this);
}
String username = getResourceConfigProperty("username");
String password = getResourceConfigProperty("password");
if (username != null && password != null) {
((HttpResolver) resolver).setBasicAuthCredentials(username, password);
} else {
log.warn("No BASIC AUTH credentials configured in order to request: " + sourcePath);
}
} else {
resolver = new SourceResolver(this);
}
Source source = null;
try {
source = resolver.resolve(sourcePath, null);
} catch(SourceException e) {
String exceptionMessage = e.getMessage();
log.error(exceptionMessage);
StringBuilder sb = new StringBuilder("<exception>" + exceptionMessage + "</exception>");
return new java.io.ByteArrayInputStream(sb.toString().getBytes());
}
try {
return org.wyona.commons.xml.XMLHelper.isWellFormed(((javax.xml.transform.stream.StreamSource) source).getInputStream());
} catch(Exception e) {
String exceptionMessage = "Data retrieved from '" + sourcePath + "' might not be well-formed (Original exception message: " + e.getMessage() + "), hence let's request it again and try to tidy it...";
log.warn(exceptionMessage);
source = resolver.resolve(sourcePath, null);
return tidy(((javax.xml.transform.stream.StreamSource) source).getInputStream());
//return tidy(intercept(((javax.xml.transform.stream.StreamSource) source).getInputStream()));
}
} catch(Exception e) {
String exceptionMessage = e.getMessage();
log.error(exceptionMessage);
StringBuilder sb = new StringBuilder("<exception>" + exceptionMessage + "</exception>");
return new java.io.ByteArrayInputStream(sb.toString().getBytes());
}
} else {
log.info("Either no protocol used or protocol not implemented: " + sourcePath);
}
Resource res = yanel.getResourceManager().getResource(getEnvironment(), getRealm(), sourcePath);
if (ResourceAttributeHelper.hasAttributeImplemented(res, "Viewable", "1")) {
// TODO: Pass the request ...
String viewV1path = getRealm().getMountPoint() + sourcePath.substring(1);
log.warn("Path of view: " + viewV1path);
View view = ((ViewableV1) res).getView(new Path(viewV1path), null);
if (view.getMimeType().indexOf("xml") >= 0) {
// TODO: Shall the mime-type be transfered?
return view.getInputStream();
}
log.error("No XML like mime-type: " + getPath() + " (" + YANEL_PATH_PROPERTY_NAME + ": " + sourcePath + ")");
} else if (ResourceAttributeHelper.hasAttributeImplemented(res, "Viewable", "2")) {
// TODO: Pass the request ...
View view = ((ViewableV2) res).getView(null);
if (view.getMimeType().indexOf("xml") >= 0) {
// TODO: Shall the mime-type be transfered?
return view.getInputStream();
}
log.error("No XML like mime-type: " + getPath() + " (" + YANEL_PATH_PROPERTY_NAME + ": " + sourcePath + ")");
} else {
log.error("Resource is not ViewableV1: " + getPath() + " (" + YANEL_PATH_PROPERTY_NAME + ": " + sourcePath + ")");
}
return null;
}
Repository repo = getRealm().getRepository();
Node node;
if (revisionName != null) {
node = repo.getNode(getPath()).getRevision(revisionName);
} else {
node = repo.getNode(getPath());
}
// TODO: Add annotations to XML root element using a namespace
String checkComments = getResourceConfigProperty("check-comments");
if (checkComments != null && checkComments.equals("true")) {
CommentManagerV1 cMan = getCommentManager();
if (cMan.hasComments(getRealm(), getPath())) {
// TODO: Differentiate between users who have access to all comments and only public comments
//java.util.List comments = cMan.getPublicComments(realm, getPath()).getComments();
java.util.List comments = cMan.getAllComments(realm, getPath()).getComments();
log.debug("Resource '" + getPath() + "' has comments: " + comments.size());
// TODO: Add comments to XML root element using a namespace
Document mainDoc = XMLHelper.readDocument(node.getInputStream());
String aggregateComments = getResourceConfigProperty("aggregate-comments");
if (aggregateComments != null && aggregateComments.equals("true")) {
Document commentsDoc = org.wyona.yarep.util.YarepXMLBindingUtil.getDocFromJAXBDataObject(cMan.getAllComments(realm, getPath()));
log.debug("Aggregate comments ...");
//org.w3c.dom.Node importedNode = mainDoc.getDocumentElement().getOwnerDocument().importNode(commentsDoc.getDocumentElement(), true);
//mainDoc.getDocumentElement().appendChild(importedNode);
//org.w3c.dom.Node importedNode = mainDoc.importNode(commentsDoc.getDocumentElement(), true);
mainDoc.getDocumentElement().appendChild(mainDoc.importNode(commentsDoc.getDocumentElement(), true));
} else {
XMLHelper.appendFragment(mainDoc.getDocumentElement(), "<comments:has-comments xmlns:comments=\"http://www.wyona.org/yanel/comments/1.0\">" + comments.size() + "</comments:has-comments>");
}
return XMLHelper.getInputStream(mainDoc, false, true, null);
} else {
log.debug("Resource '" + getPath() + "' has no comments yet.");
}
} else {
//log.debug("Do not aggregate comments");
}
return node.getInputStream();
}
/**
* Get mime type
*/
public String getMimeType(String viewId) throws Exception {
ViewDescriptor viewDescriptor = getViewDescriptor(viewId);
String mimeType;
if (viewDescriptor != null) {
mimeType = viewDescriptor.getMimeType();
if (mimeType != null) return mimeType;
}
if (viewId != null && viewId.equals(SOURCE_VIEW_ID)) {
mimeType = getResourceConfigProperty("source-view-mime-type");
if (mimeType != null) return mimeType;
}
mimeType = getResourceConfigProperty("mime-type");
if (mimeType != null) return mimeType;
String suffix = org.wyona.commons.io.PathUtil.getSuffix(getPath());
if (suffix != null) {
log.debug("SUFFIX: " + suffix);
if (suffix.equals("html")) {
//mimeType = "text/html";
mimeType = "application/xhtml+xml";
} else if (suffix.equals("xhtml")) {
mimeType = "application/xhtml+xml";
} else if (suffix.equals("xml")) {
mimeType = "application/xml";
} else {
mimeType = "application/xml";
}
} else {
mimeType = "application/xml";
}
return mimeType;
}
/**
*
*/
public Reader getReader() throws Exception {
return new InputStreamReader(getInputStream(), "UTF-8");
}
/**
*
*/
public InputStream getInputStream() throws Exception {
return getRealm().getRepository().getNode(getPath()).getInputStream();
}
/**
*
*/
public Writer getWriter() throws Exception {
log.error("Not implemented yet!");
return null;
}
/**
* @see org.wyona.yanel.core.api.attributes.ModifiableV2#getOutputStream()
*/
public OutputStream getOutputStream() throws Exception {
return getRealm().getRepository().getNode(getPath()).getOutputStream();
}
/**
* @see org.wyona.yanel.core.api.attributes.ModifiableV2#write(java.io.InputStream)
*/
public void write(InputStream in) throws Exception {
log.warn("Not implemented yet!");
}
/**
* @see org.wyona.yanel.core.api.attributes.ModifiableV2#getLastModified()
*/
public long getLastModified() throws Exception {
long lastModified;
String yanelPath = getCustomSourcePath();
if (yanelPath != null) {
log.warn("Get last modified for parameter " + YANEL_PATH_PROPERTY_NAME + " '" + yanelPath + "' is not implemented yet!");
lastModified = -1;
} else {
Node node = getRealm().getRepository().getNode(getPath());
if (node.isResource()) {
lastModified = node.getLastModified();
} else {
lastModified = 0;
}
}
return lastModified;
}
/**
* Get custom path
*/
protected String getCustomSourcePath() throws Exception {
return getResourceConfigProperty(YANEL_PATH_PROPERTY_NAME);
}
/**
* Get repository node
*/
public Node getRepoNode() throws Exception {
// INFO: The property 'yanel-path' is normally not just another repository path, but rather another resource! See getSourceXML(String) ...
String path = getCustomSourcePath();
if (path == null) {
path = getPath();
} else {
if (path.indexOf("http://") == 0 || path.indexOf("https://") == 0) {
log.warn("No local repository node: " + path);
throw new NoSuchNodeException(path, getRealm().getRepository());
}
}
return getRealm().getRepository().getNode(path);
}
/**
* org.wyona.yanel.core.api.attributes.ModifiableV2#delete()
*/
public boolean delete() throws Exception {
getRealm().getRepository().getNode(getPath()).delete();
return true;
}
/**
* @see org.wyona.yanel.core.api.attributes.VersionableV3#getRevisions(boolean)
*/
public java.util.Iterator<RevisionInformation> getRevisions(boolean reverse) throws Exception {
try {
Node node = getRepoNode();
if (org.wyona.commons.clazz.ClazzUtil.implementsInterface(node, "org.wyona.yarep.core.attributes.VersionableV1")) {
org.wyona.yarep.core.attributes.VersionableV1 versionableNode = (org.wyona.yarep.core.attributes.VersionableV1) getRepoNode();
return new org.wyona.yanel.core.attributes.versionable.RevisionInformationIterator(versionableNode.getRevisions(reverse));
} else {
return null;
}
} catch(NoSuchNodeException e) {
String path = getCustomSourcePath();
if (path != null && (path.indexOf("http://") == 0 || path.indexOf("https://") == 0)) {
log.warn("No revisions available for external URL: " + path);
return null;
} else {
throw e;
}
}
}
/**
* @see org.wyona.yanel.core.api.attributes.VersionableV2#getRevisions()
*/
public RevisionInformation[] getRevisions() throws Exception {
Revision[] revisions = getRepoNode().getRevisions();
// TODO: Use utility method/class
RevisionInformation[] revisionInfos = new RevisionInformation[revisions.length];
for (int i = 0; i < revisions.length; i++) {
revisionInfos[i] = new RevisionInformation(revisions[i]);
}
return revisionInfos;
}
/**
* @see org.wyona.yanel.core.api.attributes.VersionableV2#checkin(String)
*/
public void checkin(String comment) throws Exception {
Node node = getRealm().getRepository().getNode(getPath());
Revision revision = node.checkin(comment);
// set initial workflow state and date:
Workflow workflow = WorkflowHelper.getWorkflow(this);
if (workflow != null && revision != null) {
setWorkflowState(workflow.getInitialState(), revision.getRevisionName());
}
/*
if (node.isCheckedOut()) {
String checkoutUserID = node.getCheckoutUserID();
if (checkoutUserID.equals(userID)) {
node.checkin();
} else {
throw new Exception("Resource is checked out by another user: " + checkoutUserID);
}
} else {
throw new Exception("Resource is not checked out.");
}
*/
}
public void checkout(String userID) throws Exception {
Node node = getRealm().getRepository().getNode(getPath());
node.checkout(userID);
/*
if (node.isCheckedOut()) {
String checkoutUserID = node.getCheckoutUserID();
if (checkoutUserID.equals(userID)) {
log.warn("Resource " + getPath() + " is already checked out by this user: " + checkoutUserID);
} else {
throw new Exception("Resource is already checked out by another user: " + checkoutUserID);
}
} else {
node.checkout(userID);
}
*/
}
public void cancelCheckout() throws Exception {
Node node = getRealm().getRepository().getNode(getPath());
node.cancelCheckout();
}
/**
* Roll back to previous revision
*/
public void restore(String revisionName) throws Exception {
getRealm().getRepository().getNode(getPath()).restore(revisionName);
}
public Date getCheckoutDate() throws Exception {
log.warn("Get checkout date not implemented!");
//Node node = getRealm().getRepository().getNode(getPath());
//return node.getCheckoutDate();
return null;
}
public String getCheckoutUserID() throws Exception {
Node node = getRealm().getRepository().getNode(getPath());
return node.getCheckoutUserID();
}
/**
* @see org.wyona.yanel.core.api.attributes.VersionableV2#isCheckedOut()
*/
public boolean isCheckedOut() throws Exception {
String yanelPath = getCustomSourcePath();
if (yanelPath != null) {
log.warn("Check whether checked-out for parameter " + YANEL_PATH_PROPERTY_NAME + " '" + yanelPath + "' not implemented yet!");
return false;
} else {
Node node = getRealm().getRepository().getNode(getPath());
return node.isCheckedOut();
}
}
/**
* Get size of generated page
*/
public long getSize() throws Exception {
// TODO: If the XML is being transformed then the size will not be the same as the size of the node!
/*
Node node = getRealm().getRepository().getNode(getPath());
long size;
if (node.isResource()) {
size = node.getSize();
} else {
size = 0;
}
return size;
*/
return -1;
}
/**
* @see org.wyona.yanel.core.api.attributes.CreatableV2#create(HttpServletRequest)
*/
public void create(HttpServletRequest request) {
if (log.isDebugEnabled()) log.debug("Create XML resource ...");
try {
String title = request.getParameter("rp.title");
if (title == null || title.length() == 0) {
log.warn("No title has been specified!");
title = "No title has been specified!";
} else {
log.debug("Title: " + title);
}
Repository repo = getRealm().getRepository();
Node newNode = org.wyona.yanel.core.util.YarepUtil.addNodes(repo, getPath().toString(), org.wyona.yarep.core.NodeType.RESOURCE);
String sourceMimeType = request.getParameter("rp.source-mime-type");
if (sourceMimeType != null) {
newNode.setMimeType(sourceMimeType);
}
String templatePath = request.getParameter("rp.template");
if (log.isDebugEnabled()) log.debug("Template path: " + templatePath);
SourceResolver resolver = new SourceResolver(this);
Source src = null;
try {
if (templatePath != null) {
src = new SourceResolver(this).resolve(templatePath, null);
} else {
log.info("No template path set!");
src = null;
}
} catch(Exception e) {
log.error(e, e);
log.warn("Will fallback to hardcoded 'template'!");
src = null;
}
if (src != null) {
InputStream is = ((YanelStreamSource)src).getInputStream();
Document doc = XMLHelper.readDocument(is);
if (log.isDebugEnabled()) {
log.debug("Template content: " + System.getProperty("line.separator") + XMLHelper.documentToString(doc, false, true, null));
}
if (title != null) {
org.wyona.commons.xml.XHTMLUtil.setTitle(doc, title);
}
XMLHelper.writeDocument(doc, newNode.getOutputStream());
if (log.isDebugEnabled()) {
log.debug("Document content: " + System.getProperty("line.separator") + XMLHelper.documentToString(doc, false, true, null));
}
} else {
Writer writer = new java.io.OutputStreamWriter(newNode.getOutputStream());
writer.write("<?xml version=\"1.0\"?>");
writer.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
writer.write("<head>");
writer.write(" <title>" + title + "</title>");
writer.write(" <link rel=\"neutron-introspection\" type=\"application/neutron+xml\" href=\"?yanel.resource.usecase=introspection\"/>");
writer.write("</head>");
writer.write("<body>");
writer.write(" <h1>" + title + "</h1>");
writer.write(" <p>Edit this text by selecting an editing tool from the Yanel <a href=\"?yanel.toolbar=on\">toolbar</a>!</p>");
if (templatePath != null && src == null) {
writer.write(" <p>WARNING: There seems to be a template '" + templatePath + "' configured, but which does not exist!</p>");
}
writer.write("</body>");
writer.write("</html>");
writer.close();
}
} catch (Exception e) {
log.error(e, e);
}
}
/**
*
*/
public java.util.HashMap createRTIProperties(HttpServletRequest request) {
java.util.HashMap map = new java.util.HashMap();
map.put("xslt", request.getParameter("rp.xslt"));
map.put("mime-type", request.getParameter("rp.mime-type"));
map.put("source-view-mime-type", request.getParameter("rp.source-view-mime-type"));
map.put("workflow-schema", request.getParameter("rp.workflow-schema"));
map.put(YANEL_PATH_PROPERTY_NAME, request.getParameter("rp." + YANEL_PATH_PROPERTY_NAME));
// TODO: get all parameters, e.g. source-view-mime-type (Security!)
return map;
}
public String getCreateName(String suggestedName) {
return suggestedName;
}
/**
*
*/
public String getPropertyType(String name) {
log.warn("Not implemented yet!");
return null;
}
/**
*
*/
public Object getProperty(String name) {
log.warn("Not implemented yet!");
return null;
}
/**
*
*/
public String[] getPropertyNames() {
String[] propertyNames = new String[1];
propertyNames[0] = "title";
return propertyNames;
}
/**
*
*/
public void setProperty(String name, Object value) {
log.warn("Not implemented yet!");
}
/**
* Get introspection for Introspectable interface
*/
public String getIntrospection() throws Exception {
String name = org.wyona.commons.io.PathUtil.getName(getPath());
StringBuffer sb = new StringBuffer("<?xml version=\"1.0\"?>");
sb.append("<introspection xmlns=\"http://www.wyona.org/neutron/2.0\">");
sb.append("<navigation>");
sb.append(" <sitetree href=\"./\" method=\"PROPFIND\"/>");
sb.append("</navigation>");
sb.append("<resource name=\"" + name + "\">");
sb.append("<edit mime-type=\"" + this.getMimeType(SOURCE_VIEW_ID) + "\">");
sb.append("<checkout url=\"?yanel.resource.viewid=" + SOURCE_VIEW_ID + "&yanel.resource.usecase=checkout\" method=\"GET\"/>");
sb.append("<checkin url=\"?yanel.resource.usecase=checkin\" method=\"PUT\"/>");
sb.append("<release-lock url=\"?yanel.resource.usecase=release-lock\" method=\"GET\"/>");
sb.append("</edit>");
sb.append(getWorkflowIntrospection());
sb.append("</resource>");
sb.append("</introspection>");
return sb.toString();
}
protected TranslationManager getTranslationManager() throws TranslationException {
return getRealm().getTranslationManager();
}
public String getLanguage() throws TranslationException {
return getTranslationManager().getLanguage(this);
}
public String[] getLanguages() throws TranslationException {
return getTranslationManager().getLanguages(this);
}
public Resource getTranslation(String language) throws TranslationException {
return getTranslationManager().getTranslation(this, language);
}
public void addTranslation(Resource resource, String language) throws TranslationException {
getTranslationManager().addTranslation(this, resource, language);
}
public boolean hasTranslation(String language) throws TranslationException {
return getTranslationManager().hasTranslation(this, language);
}
public void removeTranslation(String language) throws TranslationException {
getTranslationManager().removeTranslation(this, language);
}
/************************************************
* Workflow *
************************************************/
/**
* @see org.wyona.yanel.core.api.attributes.WorkflowableV1#doTransition(String, String)
*/
public void doTransition(String transitionID, String revision) throws WorkflowException {
WorkflowHelper.doTransition(this, transitionID, revision);
}
public View getLiveView(String viewid) throws Exception {
return WorkflowHelper.getLiveView(this, viewid);
}
public boolean isLive() throws WorkflowException {
return WorkflowHelper.isLive(this);
}
public String getWorkflowVariable(String name) throws WorkflowException {
try {
return WorkflowHelper.getWorkflowVariable(getRepoNode(), name);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public void setWorkflowVariable(String name, String value) throws WorkflowException {
try {
WorkflowHelper.setWorkflowVariable(getRepoNode(), name, value);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public void removeWorkflowVariable(String name) throws WorkflowException {
try {
WorkflowHelper.removeWorkflowVariable(getRepoNode(), name);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
* @see org.wyona.yanel.core.api.attributes.WorkflowableV1#getWorkflowState(String)
*/
public String getWorkflowState(String revision) throws WorkflowException {
try {
return WorkflowHelper.getWorkflowState(getRepoNode(), revision);
//return WorkflowHelper.getWorkflowState(this, revision); // INFO: This method cannot be used in this particular case, because getRepoNode() does not return necessarily the same node as this method uses internally!
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
/**
*
*/
public void setWorkflowState(String state, String revision) throws WorkflowException {
try {
WorkflowHelper.setWorkflowState(getRepoNode(), state, revision);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public Date getWorkflowDate(String revision) throws WorkflowException {
try {
return WorkflowHelper.getWorkflowDate(getRepoNode(), revision);
} catch (Exception e) {
log.error(e, e);
throw new WorkflowException(e.getMessage(), e);
}
}
public String getWorkflowIntrospection() throws WorkflowException {
return WorkflowHelper.getWorkflowIntrospection(this);
}
/**
* Tidy HTML
* @return well-formed XHTML
*/
private InputStream tidy(InputStream in) throws Exception {
log.warn("Tidy HTML ...");
org.w3c.tidy.Tidy tidy = new org.w3c.tidy.Tidy();
tidy.setXHTML(true);
tidy.setNumEntities(true);
/*
tidy.setTidyMark(false);
tidy.setInputEncoding("utf-8");
tidy.setOutputEncoding("utf-8");
*/
java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
tidy.parse(in, out);
in.close();
return new java.io.ByteArrayInputStream(out.toByteArray());
//return intercept(new java.io.ByteArrayInputStream(out.toByteArray()));
}
/**
* Intercept InputStream and log content ...
*/
private InputStream intercept(InputStream in) throws java.io.IOException {
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
byte[] buf = new byte[8192];
int bytesR;
while ((bytesR = in.read(buf)) != -1) {
baos.write(buf, 0, bytesR);
}
// Buffer within memory (TODO: Maybe replace with File-buffering ...)
// http://www-128.ibm.com/developerworks/java/library/j-io1/
byte[] memBuffer = baos.toByteArray();
log.debug("InputStream: " + baos);
return new java.io.ByteArrayInputStream(memBuffer);
}
/**
* @see org.wyona.yanel.core.api.attributes.AnnotatableV1#setAnnotation(String)
*/
public void setAnnotation(String name) throws Exception {
readAnnotations();
if(name != null && !annotations.contains(name)){
annotations.add(name);
saveAnnotations();
} else {
log.warn("Either no annotation or annotation already exists: " + name);
}
}
/**
* @see org.wyona.yanel.core.api.attributes.AnnotatableV1#removeAnnotation(String)
*/
public void removeAnnotation(String name) throws Exception{
readAnnotations();
if(name != null && annotations.contains(name)){
annotations.remove(name);
saveAnnotations();
} else {
log.warn("Either no annotation or annotation does not exist: " + name);
}
}
/**
* @see org.wyona.yanel.core.api.attributes.AnnotatableV1#clearAllAnnotations()
*/
public void clearAllAnnotations() throws Exception{
readAnnotations();
annotations.clear();
saveAnnotations();
}
/**
* @see org.wyona.yanel.core.api.attributes.AnnotatableV1#getAnnotations()
*/
public String[] getAnnotations() throws Exception {
readAnnotations();
return annotations.toArray(new String[0]);
}
/**
* Read annotations
*/
private void readAnnotations() throws Exception {
if(annotationRead) {
log.info("Annotations have already been read from persistent repository: " + annotations);
return;
}
String yanelPath = getCustomSourcePath();
if (yanelPath != null) {
log.warn("Read annotations for parameter " + YANEL_PATH_PROPERTY_NAME + " '" + yanelPath + "' not implemented yet!");
} else {
annotations = org.wyona.yanel.core.attributes.annotatable.Util.readAnnotations(getRealm().getRepository(), getPath());
if (annotations != null) {
annotationRead = true;
return;
} else {
log.warn("No annotations yet: " + getPath());
}
}
}
/**
* Save annotations
*/
private void saveAnnotations() throws Exception{
org.wyona.yanel.core.attributes.annotatable.Util.saveAnnotations(annotations, getRealm().getRepository(), getPath());
}
/**
* @see org.wyona.yanel.core.api.attributes.CommentableV1#getCommentManager()
*/
public CommentManagerV1 getCommentManager() throws Exception {
return new org.wyona.yanel.impl.comments.CommentManagerV1Impl();
}
/**
* @see org.wyona.yanel.core.api.attributes.ViewableV2#exists()
*/
@Override
public boolean exists() throws Exception {
String sourcePath = getCustomSourcePath();
if (sourcePath != null) {
if (sourcePath.startsWith("yanelrepo:") || sourcePath.startsWith("yanelresource:") || sourcePath.startsWith("http:") || sourcePath.startsWith("https:")) {
} else {
log.info("Either no protocol used or protocol not implemented: " + sourcePath);
}
log.warn("Method exists() for custom source path '" + sourcePath + "' not implemented yet!");
return false;
} else {
return super.exists();
}
}
/**
* Check whether timeout property is configured
* @param name Name of timeout property, e.g. 'connection-timeout' or 'socket-timeout'
* @return true when timeout property is configured
*/
private boolean isTimeoutConfigured(String name) throws Exception {
if (getResourceConfigProperty(name) != null) {
return true;
} else {
return false;
}
}
/**
* Get value of timeout property
* @param name Name of timeout property, e.g. 'connection-timeout' or 'socket-timeout'
* @return value of timeout property in milliseconds
*/
private int getTimeoutValue(String name) throws Exception {
if (getResourceConfigProperty(name) != null) {
return new Integer(getResourceConfigProperty(name)).intValue();
} else {
return 0;
}
}
}