View Javadoc
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   * Returns a list of content during a given state range of the publication lifecycle.
27   *
28   * @LevelAPI Provisional
29   *
30   * @anchor LifecycleConnector
31   */
32  @Path("/authoring/")
33  public class LifecycleConnector implements ResourceContainer {
34  
35    /** The Constant LAST_MODIFIED_PROPERTY. */
36    private static final String LAST_MODIFIED_PROPERTY        = "Last-Modified";
37  
38    /** The Constant IF_MODIFIED_SINCE_DATE_FORMAT. */
39    private static final String IF_MODIFIED_SINCE_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
40  
41    /**
42     * Returns a list of content from the beginning state.
43     *
44     * {@code
45     * For example: http://localhost:8080/portal/rest/authoring/bystate/?fromstate
46     * =draft&user=root&lang=en&workspace=collaboration
47     * }
48     *
49     * @param fromstate The beginning state of the content.
50     * @param user  The author of the content.
51     * @param lang  The language of the content.
52     * @param workspace The workspace name which contains the content.
53     * @param json The format of the returned data.
54     * @return List of content.
55     * @throws Exception The exception
56     *
57     * @anchor LifecycleConnector.byState
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     * Returns a list of content from the beginning state to the last state.
71     *
72     * {@code
73     * For example: http://localhost:8080/portal/rest/authoring/tostate/?fromstate
74     * =draft&tostate=pending&user=root&lang=en&workspace=collaboration
75     * }
76     *
77     * @param fromstate  The beginning state of the content.
78     * @param tostate The destination state of the content.
79     * @param user The author of the content.
80     * @param lang The language of the content.
81     * @param workspace The workspace name which contains the content.
82     * @param json The format of the returned data.
83     * @return List of content.
84     * @throws Exception The exception
85     *
86     * @anchor LifecycleConnector.toState
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    * Returns a list of content from the given beginning state to the published state and before the given date.
101    *
102    * {@code
103    * For example: http://localhost:8080/portal/rest/authoring/bydate/?fromstate
104    * =staged&date=2&lang=en&workspace=collaboration
105    * }
106    *
107    * @param fromstate The beginning state of the content.
108    * @param date The date before when the content is published.
109    * @param workspace The workspace name which contains the content.
110    * @param lang The language of the content.
111    * @param json The format of the returned data.
112    * @return List of content.
113    * @throws Exception The exception
114    *
115    * @anchor LifecycleConnector.byDate
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    * @param fromstate
129    * @param tostate
130    * @param user
131    * @param lang
132    * @param workspace
133    * @param asJSon
134    * @return
135    * @throws Exception
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 }