1 package org.exoplatform.wcm.connector.authoring;
2
3 import java.text.DateFormat;
4 import java.text.SimpleDateFormat;
5 import java.util.Date;
6 import java.util.List;
7
8 import javax.jcr.Node;
9 import javax.ws.rs.GET;
10 import javax.ws.rs.Path;
11 import javax.ws.rs.QueryParam;
12 import javax.ws.rs.core.MediaType;
13 import javax.ws.rs.core.Response;
14 import javax.xml.parsers.DocumentBuilderFactory;
15 import javax.xml.transform.dom.DOMSource;
16
17 import org.exoplatform.services.rest.resource.ResourceContainer;
18 import org.exoplatform.services.wcm.extensions.publication.PublicationManager;
19 import org.exoplatform.services.wcm.utils.WCMCoreUtils;
20 import org.json.simple.JSONArray;
21 import org.json.simple.JSONObject;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24
25
26
27
28
29
30
31
32 @Path("/authoring/")
33 public class LifecycleConnector implements ResourceContainer {
34
35
36 private static final String LAST_MODIFIED_PROPERTY = "Last-Modified";
37
38
39 private static final String IF_MODIFIED_SINCE_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59 @GET
60 @Path("/bystate/")
61 public Response byState(@QueryParam("fromstate") String fromstate,
62 @QueryParam("user") String user,
63 @QueryParam("lang") String lang,
64 @QueryParam("workspace") String workspace,
65 @QueryParam("json") String json) throws Exception {
66 return getContents(fromstate, null, null, user, lang, workspace, json);
67 }
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @GET
89 @Path("/tostate/")
90 public Response toState(@QueryParam("fromstate") String fromstate,
91 @QueryParam("tostate") String tostate,
92 @QueryParam("user") String user,
93 @QueryParam("lang") String lang,
94 @QueryParam("workspace") String workspace,
95 @QueryParam("json") String json) throws Exception {
96 return getContents(fromstate, tostate, null, user, lang, workspace, json);
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117 @GET
118 @Path("/bydate/")
119 public Response byDate(@QueryParam("fromstate") String fromstate,
120 @QueryParam("date") String date,
121 @QueryParam("lang") String lang,
122 @QueryParam("workspace") String workspace,
123 @QueryParam("json") String json) throws Exception {
124 return getContents(fromstate, null, date, null, lang, workspace, json);
125 }
126
127
128
129
130
131
132
133
134
135
136
137 @SuppressWarnings("unchecked")
138 private Response getContents(String fromstate,
139 String tostate,
140 String date,
141 String user,
142 String lang,
143 String workspace,
144 String asJSon) throws Exception {
145 DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
146 try {
147 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
148 Element root = document.createElement("contents");
149 document.appendChild(root);
150
151 PublicationManager manager = WCMCoreUtils.getService(PublicationManager.class);
152 List<Node> nodes = manager.getContents(fromstate, tostate, date, user, lang, workspace);
153
154 JSONArray jsonList = new JSONArray();
155 for (Node node : nodes) {
156 String name = node.getName();
157 String path = node.getPath();
158 String title = null;
159 String pubDate = null;
160 if (node.hasProperty("exo:title"))
161 title = node.getProperty("exo:title").getString();
162 if (node.hasProperty("publication:startPublishedDate")) {
163 pubDate = node.getProperty("publication:startPublishedDate").getString();
164 }
165 JSONObject jsonElt = new JSONObject();
166
167 jsonElt.put("name", name);
168 if (title != null)
169 jsonElt.put("title", title);
170 jsonElt.put("path", path);
171
172 Element element = document.createElement("content");
173 element.setAttribute("name", name);
174 if (title != null)
175 element.setAttribute("title", title);
176 element.setAttribute("path", path);
177 if (pubDate != null) {
178 jsonElt.put("publishedDate", pubDate);
179 element.setAttribute("publishedDate", pubDate);
180 }
181 root.appendChild(element);
182 jsonList.add(jsonElt);
183
184 }
185
186 if ("true".equals(asJSon))
187 return Response.ok(jsonList.toString(), MediaType.TEXT_PLAIN)
188 .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
189 .build();
190 else
191 return Response.ok(new DOMSource(document), MediaType.TEXT_XML)
192 .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
193 .build();
194 } catch (Exception e) {
195 Response.serverError().build();
196 }
197 return Response.ok().header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date())).build();
198 }
199 }