-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttachment.java
More file actions
513 lines (427 loc) · 18.1 KB
/
Attachment.java
File metadata and controls
513 lines (427 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
package javaxt.exchange;
import java.io.IOException;
//******************************************************************************
//** Attachment Class
//******************************************************************************
/**
* Used to represent an attachment associated with a FolderItem. Includes
* methods to upload and download attachments.
*
******************************************************************************/
public class Attachment {
private String id;
private String cid;
private String name;
private String contentType;
private String type; //FileAttachment or ItemAttachment
private FolderItem item;
private javaxt.io.File file;
private FolderItem parent;
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using a file.
*/
public Attachment(javaxt.io.File file, FolderItem parent){
this.type = "FileAttachment";
this.file = file;
this.name = file.getName();
this.contentType = file.getContentType();
this.parent = parent;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using another Exchange item.
* @param item Item, Message, CalendarItem, Contact, Task, MeetingMessage,
* MeetingRequest, MeetingResponse, or MeetingCancellation.
*/
public Attachment(FolderItem item, FolderItem parent){
this.type = "ItemAttachment";
this.item = item;
this.parent = parent;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an attachment ID. Initiates
* a "GetAttachment" request but stops downloading and parsing the response
* as soon as it reaches the "Content" tag.
*/
public Attachment(String id, Connection conn) throws ExchangeException, java.io.IOException {
java.io.InputStream inputStream = getAttachment(id, conn);
parseResponse(inputStream, false);
inputStream.close();
this.id = id;
}
//**************************************************************************
//** Constructor
//**************************************************************************
/** Creates a new instance of this class using an XML node from a "GetItem"
* response message.
*/
protected Attachment(org.w3c.dom.Node node){
type = node.getNodeName();
if (type.contains(":")) type = type.substring(type.indexOf(":")+1);
org.w3c.dom.NodeList childNodes = node.getChildNodes();
for (int j=0; j<childNodes.getLength(); j++){
org.w3c.dom.Node childNode = childNodes.item(j);
if (childNode.getNodeType()==1){
String nodeName = childNode.getNodeName();
if (nodeName.contains(":")) nodeName = nodeName.substring(nodeName.indexOf(":")+1);
if (nodeName.equalsIgnoreCase("AttachmentId")){
id = javaxt.xml.DOM.getAttributeValue(childNode, "Id");
}
else if(nodeName.equalsIgnoreCase("Name")){
name = javaxt.xml.DOM.getNodeValue(childNode);
}
else if(nodeName.equalsIgnoreCase("ContentType")){
contentType = javaxt.xml.DOM.getNodeValue(childNode);
}
else if(nodeName.equalsIgnoreCase("ContentID")){
cid = javaxt.xml.DOM.getNodeValue(childNode);
}
}
}
}
//**************************************************************************
//** getID
//**************************************************************************
/** Returns the unique ID of the attachment.
*/
public String getID(){
return id;
}
//**************************************************************************
//** getName
//**************************************************************************
/** Returns the name of the attachment (e.g. "Resume.doc").
*/
public String getName(){
return name;
}
//**************************************************************************
//** getContentType
//**************************************************************************
/** Returns the mime type associated with the attachment (e.g. "image/jpeg").
*/
public String getContentType(){
return contentType;
}
//**************************************************************************
//** getContentID
//**************************************************************************
/** Returns the content id (cid) associated with this attachment. This is
* useful for resolving inline attachments (e.g. images) found in email
* messages, calendar invites, and other folder items.
*/
public String getContentID(){
return cid;
}
//**************************************************************************
//** toString
//**************************************************************************
/** Returns the name of the attachment (e.g. "Resume.doc").
*/
public String toString(){
return name;
}
//**************************************************************************
//** hashCode
//**************************************************************************
/** Returns the hashCode associated with the attachment ID. If the attachment
* has not been saved/uploaded, then a temporary hashCode is assigned.
*/
public int hashCode(){
if (id==null){
if (type.equals("FileAttachment")) return file.hashCode();
else return item.hashCode();
}
return (id != null) ? id.hashCode() : 0;
}
//**************************************************************************
//** equals
//**************************************************************************
public boolean equals(Object obj){
if (obj!=null){
if (obj instanceof Attachment){
return ((Attachment) obj).id.equals(id); //compare hashcodes instead?
}
}
return false;
}
//**************************************************************************
//** save
//**************************************************************************
/** Executes a "CreateAttachment" request to upload the file to the server.
*/
protected void save(Connection conn) throws ExchangeException {
if (id!=null) return; //Is it possible to update an attachment?
if (!type.equals("FileAttachment")) throw new ExchangeException("Not implemented.");
//Execute "CreateAttachment" request
javaxt.http.Request request = conn.createRequest();
request.write(new FileInputStream());
org.w3c.dom.Document xml = (org.w3c.dom.Document) conn.getResponse(request, true);
//Parse the response
org.w3c.dom.NodeList nodes = xml.getElementsByTagName("t:AttachmentId");
if (nodes!=null && nodes.getLength()>0){
id = javaxt.xml.DOM.getAttributeValue(nodes.item(0), "Id");
}
}
//**************************************************************************
//** download
//**************************************************************************
/** Used to download the attachment. Returns an input stream with the
* attachment.
*/
public java.io.InputStream download(Connection conn) throws ExchangeException {
if (!type.equals("FileAttachment")) throw new ExchangeException("Not implemented.");
try{
return parseResponse(getAttachment(id, conn), true);
}
catch(java.io.IOException e){
throw new ExchangeException(e.getLocalizedMessage());
}
}
//**************************************************************************
//** getAttachment
//**************************************************************************
/** Executes the "GetAttachment" request and returns the raw response as an
* inputstream.
*/
private java.io.InputStream getAttachment(String id, Connection conn)
throws java.io.IOException, ExchangeException {
String msg =
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
+ "<soap:Body>"
+ "<GetAttachment xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
+ "<AttachmentShape/>"
+ "<AttachmentIds><t:AttachmentId Id=\"" + id + "\"/></AttachmentIds>"
+ "</GetAttachment>"
+ "</soap:Body>"
+ "</soap:Envelope>";
javaxt.http.Response response = (javaxt.http.Response) conn.execute(msg, null, false);
String contentEncoding = response.getHeader("Content-Encoding");
if (contentEncoding!=null && contentEncoding.equalsIgnoreCase("gzip")){
return new java.util.zip.GZIPInputStream(response.getInputStream());
}
else{
return response.getInputStream();
}
}
//**************************************************************************
//** parseResponse
//**************************************************************************
/** Used to parse the "GetAttachmentResponse" response message. Instead of
* using a DOM parser, this method parses the response XML as a stream.
* This is important in order to minimize memory usage when downloading
* large attachments.
*/
private java.io.InputStream parseResponse(java.io.InputStream inputStream, boolean getContent)
throws ExchangeException, java.io.IOException {
boolean concat = true;
StringBuffer tag = new StringBuffer();
int x=0;
while ( (x = inputStream.read()) != -1) {
char c = (char) x;
if (c == '<'){
concat = true;
}
if (concat==true){
tag.append(c);
}
if (c==('>') && concat==true){
concat = false;
if (tag.indexOf("</")==-1){
String node = tag.toString().trim();
String nodeName = node.substring(1).trim();
if (nodeName.endsWith("/>")) nodeName = nodeName.substring(0, nodeName.length()-2).trim();
else nodeName = nodeName.substring(0, nodeName.length()-1).trim();
if (nodeName.contains(" ")) nodeName = nodeName.substring(0, nodeName.indexOf(" "));
if (nodeName.contains(":")) nodeName = nodeName.substring(nodeName.indexOf(":")+1);
if (nodeName.equalsIgnoreCase("Name")){
name = getNodeValue(inputStream);
}
else if(nodeName.equalsIgnoreCase("ContentType")){
contentType = getNodeValue(inputStream);
}
else if(nodeName.equalsIgnoreCase("ContentId")){
cid = getNodeValue(inputStream);
}
else if(nodeName.equalsIgnoreCase("FileAttachment")){
type = "FileAttachment";
}
else if (nodeName.equalsIgnoreCase("ItemAttachment")){
type = "ItemAttachment";
}
if (nodeName.toLowerCase().endsWith("responsemessage")){
if (node.toLowerCase().contains("error")){
if (!node.endsWith("/>")) node = node.substring(0, node.length()-1) + "/>";
org.w3c.dom.Document XMLDoc = javaxt.xml.DOM.createDocument(node);
String responseClass = javaxt.xml.DOM.getAttributeValue(XMLDoc.getFirstChild(), "ResponseClass");
if (responseClass.equalsIgnoreCase("Error")){
inputStream.close();
throw new ExchangeException("Failed to download attachment.");
}
}
}
else if(nodeName.equalsIgnoreCase("Content")){
if (getContent){
return javaxt.utils.Base64.decode(
new ContentInputStream(inputStream)
);
}
else{
return null;
}
}
}
tag = new StringBuffer();
}
}
return null;
}
//**************************************************************************
//** getNodeValue
//**************************************************************************
/** Returns the value of a node by parsing an input stream.
*/
private String getNodeValue(java.io.InputStream inputStream) throws java.io.IOException {
StringBuffer str = new StringBuffer();
int x = 0;
while ( (x = inputStream.read()) != -1) {
char c = (char) x;
if (c == '<'){
break;
}
str.append(c);
}
return str.toString();
}
//**************************************************************************
//** ContentInputStream
//**************************************************************************
/** Provides an input stream for reading the "Content" tag found in a
* "GetAttachmentResponse" message.
*/
public static class ContentInputStream extends java.io.InputStream {
private java.io.InputStream inputStream;
private boolean EOF = false;
protected ContentInputStream(java.io.InputStream inputStream){
this.inputStream = inputStream;
}
/** Returns false. This stream does not support the mark and reset
* methods.
*/
public boolean markSupported(){
return false;
}
/** Reads the next byte of data from the "Content" tag. The byte is
* returned as a positive integer. Returns a value of -1 if there is
* no more data to read (e.g. found whitespace, start of new xml tag,
* or end of stream).
*/
public int read() throws IOException {
int x = inputStream.read();
if (x==-1){
EOF = true;
return -1;
}
else{
if (EOF) return -1;
char c = (char) x;
if (c == '<'){
EOF = true;
return -1;
}
else if (c==' '){
while ( (x = inputStream.read()) != -1) {
c = (char) x;
if (c == '<'){
EOF = true;
return -1;
}
if (c != ' ') return x;
}
EOF = true;
return -1;
}
else{
return x;
}
}
}
}
//**************************************************************************
//** FileInputStream
//**************************************************************************
/** Provides an input stream with a soap message
*/
private class FileInputStream extends java.io.InputStream {
private java.io.ByteArrayInputStream a, b;
private java.io.InputStream f;
public FileInputStream(){
try{
a = new java.io.ByteArrayInputStream((
"<?xml version=\"1.0\" encoding=\"utf-8\"?>"
+ "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
+ "<soap:Body>"
+ "<CreateAttachment xmlns=\"http://schemas.microsoft.com/exchange/services/2006/messages\" xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\">"
+ "<ParentItemId Id=\"" + parent.getID() + "\" />" //ChangeKey=\"\"
+ "<Attachments>"
+ "<t:FileAttachment>"
+ "<t:Name>" + getName() + "</t:Name>"
+ "<t:ContentType>" + getContentType() + "</t:ContentType>"
+ "<t:Content>").getBytes("UTF-8"));
f = javaxt.utils.Base64.encode(file.getInputStream());
/*
<FileAttachment>
<AttachmentId/>
<Name/>
<ContentType/>
<ContentId/>
<ContentLocation/>
<Size/>
<LastModifiedTime/>
<IsInline/>
<IsContactPhoto/>
<Content/>
</FileAttachment>
*/
b = new java.io.ByteArrayInputStream((
"</t:Content>"
+ "</t:FileAttachment>"
+ "</Attachments>"
+ "</CreateAttachment>"
+ "</soap:Body>"
+ "</soap:Envelope>").getBytes("UTF-8"));
}
catch(java.io.UnsupportedEncodingException e){}
catch(java.io.IOException e){
e.printStackTrace();
}
}
/** Returns false. This stream does not support the mark and reset
* methods.
*/
public boolean markSupported(){
return false;
}
/** Returns the next byte of the soap message. */
public int read() throws IOException {
int x = a.read();
if (x!=-1) return x;
x = f.read();
if (x!=-1) return x;
x = b.read();
if (x!=-1) return x;
else{
a.close();
b.close();
f.close();
}
return -1;
}
}
}