File: WebdavResponseImpl.java

package info (click to toggle)
jackrabbit 2.12.6-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 47,432 kB
  • ctags: 46,832
  • sloc: java: 339,726; xml: 13,624; jsp: 1,883; sh: 36; makefile: 6
file content (346 lines) | stat: -rw-r--r-- 11,396 bytes parent folder | download
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.jackrabbit.webdav;

import org.apache.jackrabbit.webdav.header.CodedUrlHeader;
import org.apache.jackrabbit.webdav.header.Header;
import org.apache.jackrabbit.webdav.lock.ActiveLock;
import org.apache.jackrabbit.webdav.lock.LockDiscovery;
import org.apache.jackrabbit.webdav.observation.EventDiscovery;
import org.apache.jackrabbit.webdav.observation.ObservationConstants;
import org.apache.jackrabbit.webdav.observation.Subscription;
import org.apache.jackrabbit.webdav.observation.SubscriptionDiscovery;
import org.apache.jackrabbit.webdav.property.DavPropertySet;
import org.apache.jackrabbit.webdav.xml.DomUtil;
import org.apache.jackrabbit.webdav.xml.XmlSerializable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Locale;
import javax.servlet.ServletException;
import java.util.Collection;

/**
 * WebdavResponseImpl implements the <code>WebdavResponse</code> interface.
 */
public class WebdavResponseImpl implements WebdavResponse {

    private static Logger log = LoggerFactory.getLogger(WebdavResponseImpl.class);

    private HttpServletResponse httpResponse;

    /**
     * Create a new <code>WebdavResponse</code>
     *
     * @param httpResponse
     */
    public WebdavResponseImpl(HttpServletResponse httpResponse) {
        this(httpResponse, false);
    }

    /**
     * Create a new <code>WebdavResponse</code>
     *
     * @param httpResponse
     * @param noCache
     */
    public WebdavResponseImpl(HttpServletResponse httpResponse, boolean noCache) {
        this.httpResponse = httpResponse;
        if (noCache) {
            /* set cache control headers */
            addHeader("Pragma", "No-cache");  // http1.0
            addHeader("Cache-Control", "no-cache"); // http1.1
        }
    }

    /**
     * If the specifid exception provides an error condition an Xml response body
     * is sent providing more detailed information about the error. Otherwise only
     * the error code and status phrase is sent back.
     *
     * @param exception
     * @throws IOException
     * @see DavServletResponse#sendError(org.apache.jackrabbit.webdav.DavException)
     * @see #sendError(int, String)
     * @see #sendXmlResponse(XmlSerializable, int)
     */
    public void sendError(DavException exception) throws IOException {
        if (!exception.hasErrorCondition()) {
            httpResponse.sendError(exception.getErrorCode(), exception.getStatusPhrase());
        } else {
            sendXmlResponse(exception, exception.getErrorCode());
        }
    }

    /**
     * Send a multistatus response.
     *
     * @param multistatus
     * @throws IOException
     * @see DavServletResponse#sendMultiStatus(org.apache.jackrabbit.webdav.MultiStatus)
     */
    public void sendMultiStatus(MultiStatus multistatus) throws IOException {
        sendXmlResponse(multistatus, SC_MULTI_STATUS);
    }

    /**
     * Send response body for a lock request that was intended to refresh one
     * or several locks.
     *
     * @param locks
     * @throws java.io.IOException
     * @see DavServletResponse#sendRefreshLockResponse(org.apache.jackrabbit.webdav.lock.ActiveLock[])
     */
    public void sendRefreshLockResponse(ActiveLock[] locks) throws IOException {
        DavPropertySet propSet = new DavPropertySet();
        propSet.add(new LockDiscovery(locks));
        sendXmlResponse(propSet, SC_OK);
    }

    /**
     * Send Xml response body.
     *
     * @param serializable
     * @param status
     * @throws IOException
     * @see DavServletResponse#sendXmlResponse(XmlSerializable, int)
     */
    public void sendXmlResponse(XmlSerializable serializable, int status) throws IOException {
        httpResponse.setStatus(status);

        if (serializable != null) {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            try {
                Document doc = DomUtil.createDocument();
                doc.appendChild(serializable.toXml(doc));

                // JCR-2636: Need to use an explicit OutputStreamWriter
                // instead of relying on the built-in UTF-8 serialization
                // to avoid problems with surrogate pairs on Sun JRE 1.5.
                Writer writer = new OutputStreamWriter(out, "UTF-8");
                DomUtil.transformDocument(doc, writer);
                writer.flush();

                // TODO: Should this be application/xml? See JCR-1621
                httpResponse.setContentType("text/xml; charset=UTF-8");
                httpResponse.setContentLength(out.size());
                out.writeTo(httpResponse.getOutputStream());
            } catch (ParserConfigurationException e) {
                log.error(e.getMessage());
                throw new IOException(e.getMessage());
            } catch (TransformerException e) {
                log.error(e.getMessage());
                throw new IOException(e.getMessage());
            } catch (SAXException e) {
                log.error(e.getMessage());
                throw new IOException(e.getMessage());
            }
        }
    }

    //----------------------------< ObservationDavServletResponse Interface >---
    /**
     *
     * @param subscription
     * @throws IOException
     * @see org.apache.jackrabbit.webdav.observation.ObservationDavServletResponse#sendSubscriptionResponse(org.apache.jackrabbit.webdav.observation.Subscription)
     */
    public void sendSubscriptionResponse(Subscription subscription) throws IOException {
        String id = subscription.getSubscriptionId();
        if (id != null) {
            Header h = new CodedUrlHeader(ObservationConstants.HEADER_SUBSCRIPTIONID, id);
            httpResponse.setHeader(h.getHeaderName(), h.getHeaderValue());
        }
        DavPropertySet propSet = new DavPropertySet();
        propSet.add(new SubscriptionDiscovery(subscription));
        sendXmlResponse(propSet, SC_OK);
    }

    /**
     *
     * @param eventDiscovery
     * @throws IOException
     * @see org.apache.jackrabbit.webdav.observation.ObservationDavServletResponse#sendPollResponse(org.apache.jackrabbit.webdav.observation.EventDiscovery)
     */
    public void sendPollResponse(EventDiscovery eventDiscovery) throws IOException {
        sendXmlResponse(eventDiscovery, SC_OK);
    }

    //--------------------------------------< HttpServletResponse interface >---
    public void addCookie(Cookie cookie) {
        httpResponse.addCookie(cookie);
    }

    public boolean containsHeader(String s) {
        return httpResponse.containsHeader(s);
    }

    public String encodeURL(String s) {
        return httpResponse.encodeRedirectURL(s);
    }

    public String encodeRedirectURL(String s) {
        return httpResponse.encodeRedirectURL(s);
    }

    public String encodeUrl(String s) {
        return httpResponse.encodeUrl(s);
    }

    public String encodeRedirectUrl(String s) {
        return httpResponse.encodeRedirectURL(s);
    }

    public void sendError(int i, String s) throws IOException {
        httpResponse.sendError(i, s);
    }

    public void sendError(int i) throws IOException {
        httpResponse.sendError(i);
    }

    public void sendRedirect(String s) throws IOException {
        httpResponse.sendRedirect(s);
    }

    public void setDateHeader(String s, long l) {
        httpResponse.setDateHeader(s, l);
    }

    public void addDateHeader(String s, long l) {
        httpResponse.addDateHeader(s, l);
    }

    public void setHeader(String s, String s1) {
        httpResponse.setHeader(s, s1);
    }

    public void addHeader(String s, String s1) {
        httpResponse.addHeader(s, s1);
    }

    public void setIntHeader(String s, int i) {
        httpResponse.setIntHeader(s, i);
    }

    public void addIntHeader(String s, int i) {
        httpResponse.addIntHeader(s, i);
    }

    public void setStatus(int i) {
        httpResponse.setStatus(i);
    }

    public void setStatus(int i, String s) {
        httpResponse.setStatus(i, s);
    }

    public String getCharacterEncoding() {
        return httpResponse.getCharacterEncoding();
    }

    public ServletOutputStream getOutputStream() throws IOException {
        return httpResponse.getOutputStream();
    }

    public PrintWriter getWriter() throws IOException {
        return httpResponse.getWriter();
    }

    public void setContentLength(int i) {
        httpResponse.setContentLength(i);
    }

    public void setContentType(String s) {
        httpResponse.setContentType(s);
    }

    public void setBufferSize(int i) {
        httpResponse.setBufferSize(i);
    }

    public int getBufferSize() {
        return httpResponse.getBufferSize();
    }

    public void flushBuffer() throws IOException {
        httpResponse.flushBuffer();
    }

    public void resetBuffer() {
        httpResponse.resetBuffer();
    }

    public boolean isCommitted() {
        return httpResponse.isCommitted();
    }

    public void reset() {
        httpResponse.reset();
    }

    public void setLocale(Locale locale) {
        httpResponse.setLocale(locale);
    }

    public Locale getLocale() {
        return httpResponse.getLocale();
    }
    // Compat Servlet 2.4
    public void setCharacterEncoding(String env) {
       httpResponse.setCharacterEncoding(env);
    }
    public String getContentType() {
       return httpResponse.getContentType();
    }
    //---< servlet 3.0 >---
    public Collection<String> getHeaderNames() {
        throw new UnsupportedOperationException("Not supported.");
    }

    public String getHeader(String name) {
        throw new UnsupportedOperationException("Not supported.");
    }

    public Collection<String> getHeaders(String name) {
        throw new UnsupportedOperationException("Not supported.");
    }

    public void logout() throws ServletException {
        throw new UnsupportedOperationException("Not supported.");
    }

    public int getStatus() {
        throw new UnsupportedOperationException("Not supported.");
    }

    public void setContentLengthLong(long length) {
        throw new UnsupportedOperationException("Not supported.");
    }
}