Sync to my latest dev work
[jgroupdav.git] / src / net / bionicmessage / groupdav / GroupDAVObject.java
blob2ccef9d2c9ec9b989ff2a372a3bde353226e9fa4
1 /* BionicMessage.net Java GroupDAV library V0.9
2 * GroupDAVObject.java
4 * Created on 9 April 2006, 13:39
6 * Copyright (c) 2006 Mathew McBride / "BionicMessage.net"
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
26 package net.bionicmessage.groupdav;
28 import java.util.Hashtable;
30 /**
31 * A simple class to represent a GroupDAV object
32 * @author matt
34 public class GroupDAVObject implements java.io.Serializable {
35 private Hashtable headers = null;
36 private String content = "";
37 private int status = 0;
38 private String etag = null;
39 /** Represents an object which has been fetched from the server
40 * in the cycle of the last operation */
41 public static final int OBJECT_GET = 0;
42 /** Represents an object which was either created or modified on the server
43 * in the cycle of the last operation */
44 public static final int OBJECT_PUT = 1;
45 /** Represents an object which was deleted from the server in the cycle of
46 * the last operation */
47 public static final int OBJECT_KILLED = 2;
48 private String loc = null;
49 /**
50 * Creates a new instance of GroupDAVObject
51 * @param httpoutput The full HTTP output of the last operation, from HTTP/1.x .... to the last byte
52 * of content
53 * @param status An integer (either of OBJECT_GET,OBJECT_PUT or OBJECT_KILLED)
54 * representing the operation that was performed
56 * {@link #OBJECT_GET OBJECT_GET}
57 * {@link #OBJECT_PUT OBJECT_PUT}
58 * {@link #OBJECT_KILLED OBJECT_KILLED}
60 public GroupDAVObject(String httpoutput, int status) {
61 headers = new Hashtable();
62 extractHeaders(httpoutput);
63 etag = "";
64 if (headers.get("etag") != null) {
65 etag = (String)headers.get("etag");
66 etag = etag.trim();
68 if (headers.get("location") != null) {
69 loc = (String)headers.get("location");
70 loc = loc.trim();
72 this.status = status;
74 private void extractHeaders(String httpoutput) {
75 String[] split = httpoutput.split("\n");
76 boolean contentMode = false;
77 // StringBuffer ctent = new StringBuffer();
78 for (int i = 0; i < split.length; i++) {
79 String line = split[i];
80 String[] httpheader = line.split(":");
81 if (httpheader.length == 1 || contentMode) {
82 if (httpheader[0].indexOf("HTTP/1.1") == -1) {
83 // contentMode = true;
84 // ctent.append(line);
85 // ctent.append("\r\n");
87 } else {
88 String h0 = httpheader[0].toLowerCase(); // nullify mixed case mess
89 line = line.replace(httpheader[0]+":","");
90 headers.put(h0,line);
93 String[] csplit = httpoutput.split("\r\n\r\n");
94 if (csplit.length > 1) {
95 // fix for situations where some lines are broken up too much
96 for (int i = 1; i < csplit.length; i++) {
97 content += csplit[i]+"\r\n"; // also assume if it goes too far,
98 // we lost the brek
104 * Get a {@link java.util.Hashtable Hashtable} of HTTP headers for the last request.
105 * All header names have been lowercased.
106 * @return A {@link java.util.Hashtable Hashtable} of HTTP headers.
108 public Hashtable getHeaders() {
109 return headers;
112 * Return the content of the object, if any.
113 * @return The object of the content, or null if there was no content.
115 public String getContent() {
116 return content.trim();
119 * Returns the HTTP Content-Type for a GET operation
120 * @return HTTP Content Type of the content, or null if not applicable
122 public String getType() {
123 return headers.get("content-type").toString();
126 * Set the eTag attribute for this object
127 * @param etag eTag attribute
129 public void setEtag(String etag) {
130 this.etag = etag;
134 * Get the eTag attribute for this object
135 * @return the eTag for this object
137 public String getEtag() {
138 return etag;
142 * Set the content for this object
143 * @param content A String containing the new content for this object
145 public void setContent(String content) {
146 this.content = content;
149 * Get the server location for this object
150 * @return The server location for this object, or null if not
151 * known
153 public String getLocation() {
154 return loc;
157 * Set the server location for this object
158 * @param loc The server location of this object
160 public void setLocation(String loc) {
161 this.loc = loc;
163 /** Get the status code for this object
164 * @return The status code for this object */
165 public int getStatus() {
166 return status;