View Javadoc
1   package org.exoplatform.wcm.connector.authoring;
2   
3   import java.io.File;
4   import java.io.FileOutputStream;
5   import java.io.OutputStream;
6   import java.text.DateFormat;
7   import java.text.SimpleDateFormat;
8   import java.util.Date;
9   
10  import javax.ws.rs.POST;
11  import javax.ws.rs.Path;
12  import javax.ws.rs.core.Response;
13  
14  import org.exoplatform.container.xml.InitParams;
15  import org.exoplatform.services.log.ExoLogger;
16  import org.exoplatform.services.log.Log;
17  import org.exoplatform.services.rest.resource.ResourceContainer;
18  import org.exoplatform.services.wcm.extensions.security.SHAMessageDigester;
19  
20  /**
21   * Copies a file.
22   *
23   * @LevelAPI Provisional
24   * 
25   * @anchor CopyContentFile
26   */
27  @Path("/copyfile/")
28  public class CopyContentFile implements ResourceContainer {
29  
30    private static final Log    LOG                           = ExoLogger.getLogger(CopyContentFile.class.getName());
31    private static final String OK_RESPONSE                   = "OK";
32  
33    private static final String KO_RESPONSE                   = "KO";
34  
35    private String stagingStorage;
36  
37    private String targetKey;
38  
39    /** The Constant LAST_MODIFIED_PROPERTY. */
40    private static final String LAST_MODIFIED_PROPERTY        = "Last-Modified";
41  
42    /** The Constant IF_MODIFIED_SINCE_DATE_FORMAT. */
43    private static final String IF_MODIFIED_SINCE_DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss z";
44  
45    public CopyContentFile(InitParams params) {
46      stagingStorage = params.getValueParam("stagingStorage").getValue();
47      targetKey = params.getValueParam("targetKey").getValue();
48    }
49  
50    /**
51    * Copies a file.
52    * 
53    * @param param The file path.
54    * @return Response inputstream.
55    * @throws Exception The exception
56    * 
57    * @anchor CopyContentFile.copyFile
58    */
59    @POST
60    @Path("/copy/")
61    public Response copyFile(String param) throws Exception {
62      if (LOG.isDebugEnabled()) {
63        LOG.debug("Start Execute CopyContentFile Web Service");
64      }
65  
66      DateFormat dateFormat = new SimpleDateFormat(IF_MODIFIED_SINCE_DATE_FORMAT);
67      try {
68  
69        String[] tabParam = param.split("&&");
70        String timesTamp = tabParam[0].split("=")[1];
71        String clientHash = tabParam[1].split("=")[1];
72        String contents = tabParam[2].split("contentsfile=")[1];
73        String[] tab = targetKey.split("$TIMESTAMP");
74        StringBuffer resultKey = new StringBuffer();
75        for (int k = 0; k < tab.length; k++) {
76          resultKey.append(tab[k]);
77          if (k != (tab.length - 1))
78            resultKey.append(timesTamp);
79        }
80        String serverHash = SHAMessageDigester.getHash(resultKey.toString());
81        if (serverHash != null && serverHash.equals(clientHash)) {
82          Date date = new Date();
83          long time = date.getTime();
84          File stagingFolder = new File(stagingStorage);
85          if (!stagingFolder.exists())
86            stagingFolder.mkdirs();
87          File contentsFile = new File(stagingStorage + File.separator + clientHash + "-" + time
88              + ".xml");
89          OutputStream ops = new FileOutputStream(contentsFile);
90          ops.write(contents.getBytes());
91          ops.close();
92        } else {
93          if (LOG.isWarnEnabled()) {
94            LOG.warn("Anthentification failed...");
95          }
96          return Response.ok(KO_RESPONSE + "...Anthentification failed", "text/plain")
97                                 .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date())).build();
98        }
99      } catch (Exception ex) {
100       if (LOG.isErrorEnabled()) {
101         LOG.error("error when copying content file" + ex.getMessage());
102       }
103       return Response.ok(KO_RESPONSE + "..." + ex.getMessage(), "text/plain")
104                      .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
105                      .build();
106     }
107     if (LOG.isDebugEnabled()) {
108       LOG.debug("Start Execute CopyContentFile Web Service");
109     }
110     return Response.ok(OK_RESPONSE
111                            + "...content has been successfully copied in the production server",
112                        "text/plain")
113                    .header(LAST_MODIFIED_PROPERTY, dateFormat.format(new Date()))
114                    .build();
115 
116   }
117 
118 }