1 23 24 package org.apache.webdav.lib.methods; 25 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import org.apache.commons.httpclient.HttpConnection; 29 import org.apache.commons.httpclient.HttpException; 30 import org.apache.commons.httpclient.HttpState; 31 import org.apache.webdav.lib.util.WebdavStatus; 32 import org.apache.webdav.lib.util.XMLPrinter; 33 34 35 59 public class LabelMethod extends XMLResponseMethodBase { 60 61 63 66 public static final int LABEL_SET = 1; 67 68 69 72 public static final int LABEL_ADD = 2; 73 74 75 78 public static final int LABEL_REMOVE = 3; 79 80 81 84 private String labelName = null; 85 86 87 90 private int type = 0; 91 92 94 97 public LabelMethod() { 98 } 99 100 101 108 public LabelMethod(String path, int action, String labelName) { 109 super(path); 110 this.labelName = labelName; 111 this.type = action; 112 } 113 114 115 120 public void setType(int type) { 121 this.type = type; 122 } 123 124 125 130 public int getType() { 131 return type; 132 } 133 134 135 140 public void setLabelName(String labelName) { 141 this.labelName = labelName; 142 } 143 144 145 150 public String getLabelName() { 151 return labelName; 152 } 153 154 155 161 public void addRequestHeaders(HttpState state, HttpConnection conn) 162 throws IOException , HttpException { 163 164 if (getRequestHeader("Content-Type") == null ) super.setRequestHeader("Content-Type", "text/xml; charset=utf-8"); 166 super.addRequestHeaders(state, conn); 167 } 168 169 175 protected String generateRequestBody() { 176 177 if (type <= 0 || labelName == null) 178 throw new IllegalStateException 179 ("Action type and label name must be set before " + 180 "calling this function"); 181 182 XMLPrinter printer = new XMLPrinter(); 183 184 printer.writeXMLHeader(); 185 printer.writeElement("D", "DAV:", "label", XMLPrinter.OPENING); 186 187 String typeElement = null; 188 switch (type) { 189 case LABEL_SET: 190 typeElement = "set"; 191 break; 192 case LABEL_REMOVE: 193 typeElement = "remove"; 194 break; 195 case LABEL_ADD: 196 typeElement = "add"; 197 break; 198 } 199 200 printer.writeElement("D", typeElement, XMLPrinter.OPENING); 201 printer.writeElement("D", "label-name", XMLPrinter.OPENING); 202 printer.writeText(labelName); 203 printer.writeElement("D", "label-name", XMLPrinter.CLOSING); 204 printer.writeElement("D", typeElement, XMLPrinter.CLOSING); 205 printer.writeElement("D", "label", XMLPrinter.CLOSING); 206 207 return printer.toString(); 208 } 209 210 215 public void parseResponse(InputStream input, HttpState state, HttpConnection conn) 216 throws IOException , HttpException { 217 try 218 { 219 int code = getStatusLine().getStatusCode(); 220 if (code == WebdavStatus.SC_CONFLICT || 221 code == WebdavStatus.SC_MULTI_STATUS || 222 code == WebdavStatus.SC_FORBIDDEN ) { 223 parseXMLResponse(input); 224 } 225 } 226 catch (IOException e) { 227 } 229 } 230 231 public String getName() { 232 return "LABEL"; 233 } 234 } 235 236 237 | Popular Tags |