Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8cc6f8e5f52163cbdb17150425857f97b4f1b087 (plain) (blame)
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
//
//  ========================================================================
//  Copyright (c) 1995-2012 Mort Bay Consulting Pty. Ltd.
//  ------------------------------------------------------------------------
//  All rights reserved. This program and the accompanying materials
//  are made available under the terms of the Eclipse Public License v1.0
//  and Apache License v2.0 which accompanies this distribution.
//
//      The Eclipse Public License is available at
//      http://www.eclipse.org/legal/epl-v10.html
//
//      The Apache License v2.0 is available at
//      http://www.opensource.org/licenses/apache2.0.php
//
//  You may elect to redistribute this code under either of these licenses.
//  ========================================================================
//


package org.eclipse.jetty.client.webdav;

import java.io.IOException;

import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.HttpEventListenerWrapper;
import org.eclipse.jetty.client.HttpExchange;
import org.eclipse.jetty.client.security.SecurityListener;
import org.eclipse.jetty.http.HttpMethods;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.Buffer;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

/**
 * WebdavListener
 * 
 * 
 * 
 * 
 */
public class WebdavListener extends HttpEventListenerWrapper
{
    private static final Logger LOG = Log.getLogger(WebdavListener.class);

    private HttpDestination _destination;
    private HttpExchange _exchange;
    private boolean _requestComplete;
    private boolean _responseComplete; 
    private boolean _webdavEnabled;
    private boolean _needIntercept;

    public WebdavListener(HttpDestination destination, HttpExchange ex)
    {
        // Start of sending events through to the wrapped listener
        // Next decision point is the onResponseStatus
        super(ex.getEventListener(),true);
        _destination=destination;
        _exchange=ex;

        // We'll only enable webdav if this is a PUT request
        if ( HttpMethods.PUT.equalsIgnoreCase( _exchange.getMethod() ) )
        {
            _webdavEnabled = true;
        }
    }

    @Override
    public void onResponseStatus(Buffer version, int status, Buffer reason) throws IOException
    {
        if ( !_webdavEnabled )
        {
            _needIntercept = false;
            super.onResponseStatus(version, status, reason);
            return;
        }
        
        if (LOG.isDebugEnabled())
            LOG.debug("WebdavListener:Response Status: " + status );

        // The dav spec says that CONFLICT should be returned when the parent collection doesn't exist but I am seeing
        // FORBIDDEN returned instead so running with that.
        if ( status == HttpStatus.FORBIDDEN_403 || status == HttpStatus.CONFLICT_409 )
        {
            if ( _webdavEnabled )
            {
                if (LOG.isDebugEnabled())
                    LOG.debug("WebdavListener:Response Status: dav enabled, taking a stab at resolving put issue" );
                setDelegatingResponses( false ); // stop delegating, we can try and fix this request
                _needIntercept = true;
            }
            else
            {
                if (LOG.isDebugEnabled())
                    LOG.debug("WebdavListener:Response Status: Webdav Disabled" );
                setDelegatingResponses( true ); // just make sure we delegate
                setDelegatingRequests( true );
                _needIntercept = false;
            }
        }
        else
        {
            _needIntercept = false;
            setDelegatingResponses( true );
            setDelegatingRequests( true );
        }

        super.onResponseStatus(version, status, reason);
    }

    @Override
    public void onResponseComplete() throws IOException
    {
        _responseComplete = true;
        if (_needIntercept)
        {
            if ( _requestComplete && _responseComplete)
            {
                try
                {
                    // we have some work to do before retrying this
                    if ( resolveCollectionIssues() )
                    {
                        setDelegatingRequests( true );
                        setDelegatingResponses(true);
                        _requestComplete = false;
                        _responseComplete = false;
                        _destination.resend(_exchange);
                    }
                    else
                    {
                        // admit defeat but retry because someone else might have 
                        setDelegationResult(false);
                        setDelegatingRequests( true );
                        setDelegatingResponses(true);
                        super.onResponseComplete();
                    }
                }
                catch ( IOException ioe )
                {
                    LOG.debug("WebdavListener:Complete:IOException: might not be dealing with dav server, delegate");
                    super.onResponseComplete();
                }
            }
            else
            {
                if (LOG.isDebugEnabled())
                    LOG.debug("WebdavListener:Not ready, calling super");
                super.onResponseComplete();
            }
        }
        else
        {
            super.onResponseComplete();
        }
    }

    
    
    @Override
    public void onRequestComplete () throws IOException
    {
        _requestComplete = true;
        if (_needIntercept)
        {
            if ( _requestComplete && _responseComplete)
            {
                try
                {
                    // we have some work to do before retrying this
                    if ( resolveCollectionIssues() )
                    {
                        setDelegatingRequests( true );
                        setDelegatingResponses(true);
                        _requestComplete = false;
                        _responseComplete = false;
                        _destination.resend(_exchange);
                    }
                    else
                    {
                        // admit defeat but retry because someone else might have 
                        setDelegatingRequests( true );
                        setDelegatingResponses(true);
                        super.onRequestComplete();
                    }
                }
                catch ( IOException ioe )
                {
                    LOG.debug("WebdavListener:Complete:IOException: might not be dealing with dav server, delegate");
                    super.onRequestComplete();
                }
            }
            else
            {
                if (LOG.isDebugEnabled())
                    LOG.debug("WebdavListener:Not ready, calling super");
                super.onRequestComplete();
            }
        }
        else
        {
            super.onRequestComplete();
        } 
    }

   
    
    
    /**
     * walk through the steps to try and resolve missing parent collection issues via webdav
     *
     * TODO this really ought to use URI itself for this resolution
     *
     * @return
     * @throws IOException
     */
    private boolean resolveCollectionIssues() throws IOException
    {

        String uri = _exchange.getURI();
        String[] uriCollection = _exchange.getURI().split("/");
        int checkNum = uriCollection.length;
        int rewind = 0;

        String parentUri = URIUtil.parentPath( uri );
        while ( parentUri != null && !checkExists(parentUri) )
        {
            ++rewind;
            parentUri = URIUtil.parentPath( parentUri );
        }

        // confirm webdav is supported for this collection
        if ( checkWebdavSupported() )
        {
            for (int i = 0; i < rewind;)
            {
                makeCollection(parentUri + "/" + uriCollection[checkNum - rewind - 1]);
                parentUri = parentUri + "/" + uriCollection[checkNum - rewind - 1];
                --rewind;
            }
        }
        else
        {
            return false;
        }

        return true;
    }

    private boolean checkExists( String uri ) throws IOException
    {
        if (uri == null)
        {
            System.out.println("have failed miserably");
            return false;
        }
        
        PropfindExchange propfindExchange = new PropfindExchange();
        propfindExchange.setAddress( _exchange.getAddress() );
        propfindExchange.setMethod( HttpMethods.GET ); // PROPFIND acts wonky, just use get
        propfindExchange.setScheme( _exchange.getScheme() );
        propfindExchange.setEventListener( new SecurityListener( _destination, propfindExchange ) );
        propfindExchange.setConfigureListeners( false );
        propfindExchange.setRequestURI( uri );

        _destination.send( propfindExchange );

        try
        {
            propfindExchange.waitForDone();

            return propfindExchange.exists();
        }
        catch ( InterruptedException ie )
        {
            LOG.ignore( ie );                  
            return false;
        }
    }

    private boolean makeCollection( String uri ) throws IOException
    {
        MkcolExchange mkcolExchange = new MkcolExchange();
        mkcolExchange.setAddress( _exchange.getAddress() );
        mkcolExchange.setMethod( "MKCOL " + uri + " HTTP/1.1" );
        mkcolExchange.setScheme( _exchange.getScheme() );
        mkcolExchange.setEventListener( new SecurityListener( _destination, mkcolExchange ) );
        mkcolExchange.setConfigureListeners( false );
        mkcolExchange.setRequestURI( uri );

        _destination.send( mkcolExchange );

        try
        {
            mkcolExchange.waitForDone();

            return mkcolExchange.exists();
        }
        catch ( InterruptedException ie )
        {
            LOG.ignore( ie );
            return false;
        }
    }

    
    private boolean checkWebdavSupported() throws IOException
    {
        WebdavSupportedExchange supportedExchange = new WebdavSupportedExchange();
        supportedExchange.setAddress( _exchange.getAddress() );
        supportedExchange.setMethod( HttpMethods.OPTIONS );
        supportedExchange.setScheme( _exchange.getScheme() );
        supportedExchange.setEventListener( new SecurityListener( _destination, supportedExchange ) );
        supportedExchange.setConfigureListeners( false );
        supportedExchange.setRequestURI( _exchange.getURI() );

        _destination.send( supportedExchange );

        try
        {
            supportedExchange.waitTilCompletion();
            return supportedExchange.isWebdavSupported();
        }
        catch (InterruptedException ie )
        {            
            LOG.ignore( ie );
            return false;
        }

    }

}

Back to the top