KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > publisher > clientimpl > BlobInfoImpl


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.outerj.daisy.publisher.clientimpl;
17
18 import org.outerj.daisy.publisher.BlobInfo;
19 import org.outerj.daisy.repository.RepositoryException;
20 import org.apache.commons.httpclient.methods.GetMethod;
21 import org.apache.commons.httpclient.util.DateParser;
22 import org.apache.commons.httpclient.util.DateParseException;
23
24 import java.util.Date JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27
28 public class BlobInfoImpl implements BlobInfo {
29     private final GetMethod method;
30     private final Date JavaDoc lastModified;
31     private final String JavaDoc mimeType;
32     private final long size;
33     private boolean disposed = false;
34     private static final String JavaDoc DISPOSED_MESSAGE = "This BlobInfo object has been disposed.";
35
36     public BlobInfoImpl(GetMethod method) throws RepositoryException {
37         this.method = method;
38         try {
39             this.lastModified = DateParser.parseDate(method.getResponseHeader("Last-Modified").getValue());
40         } catch (DateParseException e) {
41             throw new RepositoryException(e);
42         }
43         this.mimeType = method.getResponseHeader("Content-Type").getValue();
44         this.size = Integer.parseInt(method.getResponseHeader("Content-Length").getValue());
45     }
46
47     public Date JavaDoc getLastModified() {
48         if (disposed)
49             throw new RuntimeException JavaDoc(DISPOSED_MESSAGE);
50         return lastModified;
51     }
52
53     public String JavaDoc getMimeType() {
54         if (disposed)
55             throw new RuntimeException JavaDoc(DISPOSED_MESSAGE);
56         return mimeType;
57     }
58
59     public long getSize() {
60         if (disposed)
61             throw new RuntimeException JavaDoc(DISPOSED_MESSAGE);
62         return size;
63     }
64
65     public InputStream JavaDoc getInputStream() throws RepositoryException, IOException JavaDoc {
66         if (disposed)
67             throw new RuntimeException JavaDoc(DISPOSED_MESSAGE);
68         return method.getResponseBodyAsStream();
69     }
70
71     public void dispose() {
72         method.releaseConnection();
73         this.disposed = true;
74     }
75 }
76
Popular Tags