View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This program is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU Affero General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  
18  package org.exoplatform.calendar.ws.bean;
19  
20  import static org.exoplatform.calendar.ws.CalendarRestApi.ATTACHMENT_URI;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  
25  import org.exoplatform.calendar.service.Attachment;
26  import org.exoplatform.calendar.ws.common.Resource;
27  import org.exoplatform.services.log.ExoLogger;
28  import org.exoplatform.services.log.Log;
29  
30  public class AttachmentResource extends Resource {
31    private static final long serialVersionUID = -9218103606107024398L;
32  
33    private static final Log LOG = ExoLogger.getExoLogger(AttachmentResource.class);
34  
35    private String name;
36    private String mimeType; 
37    private long weight;
38    
39    public AttachmentResource() {
40      super(null);
41    } 
42  
43    public AttachmentResource(Attachment data, String basePath) {
44      super(encode(data.getDataPath()));
45      
46      StringBuilder path = new StringBuilder(basePath);
47      path.append(ATTACHMENT_URI);
48      try {      
49        setHref(path.toString() + getId());
50      } catch (Exception e) {
51        LOG.error(e);
52      }
53      
54      name = data.getName();
55      mimeType = data.getMimeType();
56      weight = data.getSize();
57    }
58  
59    /**
60     * we can't use / character in the path due the the bug of tomcat 
61     * that doesn't allow %2F 
62     */
63    public static String encode(String id) {
64      id = id.replace("/", "::");
65      URI uri;
66      try {
67        uri = new URI("http", "", "/" + id, "");
68        /*
69         * http:///{id}
70         */
71        return uri.toASCIIString().substring(8);
72      } catch (URISyntaxException e) {
73        LOG.error(e.getMessage(), e);
74        return null;
75      }    
76    }  
77    
78    /**
79     * we can't use / character in the path due the the bug of tomcat 
80     * that doesn't allow %2F
81     */
82    public static String decode(String path) {
83      return path.replace("::", "/");
84    }
85  
86    public String getName() {
87      return name;
88    }
89  
90    public String getMimeType() {
91      return mimeType;
92    }
93  
94    public long getWeight() {
95      return weight;
96    }  
97  }