KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > james > imapserver > client > fetch > FetchBody


1 /****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one *
3  * or more contributor license agreements. See the NOTICE file *
4  * distributed with this work for additional information *
5  * regarding copyright ownership. The ASF licenses this file *
6  * to you under the Apache License, Version 2.0 (the *
7  * "License"); you may not use this file except in compliance *
8  * with the License. You may obtain a copy of the License at *
9  * *
10  * http://www.apache.org/licenses/LICENSE-2.0 *
11  * *
12  * Unless required by applicable law or agreed to in writing, *
13  * software distributed under the License is distributed on an *
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
15  * KIND, either express or implied. See the License for the *
16  * specific language governing permissions and limitations *
17  * under the License. *
18  ****************************************************************/

19
20 package org.apache.james.imapserver.client.fetch;
21
22 import java.io.IOException JavaDoc;
23
24 import javax.mail.MessagingException JavaDoc;
25 import javax.mail.internet.MimeMessage JavaDoc;
26
27 import org.apache.james.imapserver.util.MessageGenerator;
28
29 public class FetchBody {
30
31     final boolean peek;
32
33     private FetchHeader fetchHeader;
34
35     public FetchBody(boolean peek) {
36         this.peek = peek;
37     }
38
39     public String JavaDoc getCommand() {
40         String JavaDoc result= "";
41         if (peek) {
42             result += "BODY.PEEK[";
43         } else {
44             result += "BODY[";
45         }
46         if (fetchHeader!=null) {
47             result += fetchHeader.getCommand();
48         }
49         result += "]";
50         return result;
51     }
52
53     public String JavaDoc getResult(MimeMessage JavaDoc m) throws IOException JavaDoc,
54             MessagingException JavaDoc {
55         // TODO decide whether it should be BODY.PEEK when peek!
56
String JavaDoc result = "BODY[";
57         final String JavaDoc data;
58         if (fetchHeader != null) {
59             result += fetchHeader.getCommand();
60             data = fetchHeader.getData(m);
61         } else {
62             data = getData(m);
63         }
64         result += "] {" + data.length() + "}\r\n" + data;
65         // TODO Shouldn't we append another CRLF?
66
return result;
67     }
68
69     private String JavaDoc getData(MimeMessage JavaDoc m) throws IOException JavaDoc,
70             MessagingException JavaDoc {
71         String JavaDoc data = MessageGenerator.messageContentToString(m);
72         return data;
73     }
74
75     public void setFetchHeader(FetchHeader fetchHeader) {
76         this.fetchHeader = fetchHeader;
77
78     }
79
80 }
81
Popular Tags