1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
61
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
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
80
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 }