Statistics
| Branch: | Tag: | Revision:

root / src / org / gss_project / gss / server / webdav / milton / GssWebDavResponseHandler.java @ 1206:292dec4eae08

History | View | Annotate | Download (3.8 kB)

1
/*
2
 * Copyright 2011 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package org.gss_project.gss.server.webdav.milton;
20

    
21
import java.util.Date;
22
import java.util.Map;
23

    
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
import com.bradmcevoy.http.AbstractWrappingResponseHandler;
28
import com.bradmcevoy.http.Auth;
29
import com.bradmcevoy.http.GetableResource;
30
import com.bradmcevoy.http.Request;
31
import com.bradmcevoy.http.Resource;
32
import com.bradmcevoy.http.Response;
33
import com.bradmcevoy.http.exceptions.BadRequestException;
34
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
35
import com.bradmcevoy.http.http11.DefaultHttp11ResponseHandler;
36
import com.bradmcevoy.http.http11.DefaultHttp11ResponseHandler.BUFFERING;
37
import com.bradmcevoy.http.webdav.DefaultWebDavResponseHandler;
38
import com.bradmcevoy.http.webdav.WebDavResponseHandler;
39

    
40

    
41
/**
42
 * @author kman
43
 *
44
 */
45
public class GssWebDavResponseHandler extends AbstractWrappingResponseHandler {
46

    
47
    private static final Logger log = LoggerFactory.getLogger( GssWebDavResponseHandler.class );
48
    
49
    
50

    
51
    public GssWebDavResponseHandler() {
52
    }
53

    
54
    public GssWebDavResponseHandler( WebDavResponseHandler wrapped ) {
55
        super( wrapped );
56
        if(wrapped instanceof DefaultWebDavResponseHandler){
57
                ((DefaultWebDavResponseHandler)wrapped).setBuffering(BUFFERING.always);
58
        }
59
    }
60

    
61
   
62

    
63
    @Override
64
    public void respondContent( Resource resource, Response response, Request request, Map<String, String> params ) throws NotAuthorizedException, BadRequestException {
65
        if( resource instanceof GetableResource ) {
66
            GetableResource r = (GetableResource) resource;
67
            log.trace( "respondContent: not compressable" );
68
            response.setVaryHeader( "Accept-Encoding" );
69
            wrapped.respondContent( resource, response, request, params );
70
            
71
        } else {
72
            throw new RuntimeException( "Cant generate content for non-Getable resource: " + resource.getClass() );
73
        }
74
    }
75

    
76
    protected void setRespondContentCommonHeaders( Response response, Resource resource, Response.Status status, Auth auth ) {
77
        response.setStatus( status );
78
        response.setDateHeader( new Date() );
79
        String etag = wrapped.generateEtag( resource );
80
        if( etag != null ) {
81
            response.setEtag( etag );
82
        }
83
        DefaultHttp11ResponseHandler.setModifiedDate( response, resource, auth );
84
    }
85

    
86
    private boolean canCompress( GetableResource r, String contentType, String acceptableEncodings ) {
87
        log.trace( "canCompress: contentType: " + contentType + " acceptable-encodings: " + acceptableEncodings );
88
        if( contentType != null ) {
89
            contentType = contentType.toLowerCase();
90
            boolean contentIsCompressable = contentType.contains( "text" ) || contentType.contains( "css" ) || contentType.contains( "js" ) || contentType.contains( "javascript" );
91
            if( contentIsCompressable ) {
92
                boolean supportsGzip = ( acceptableEncodings != null && acceptableEncodings.toLowerCase().indexOf( "gzip" ) > -1 );
93
                log.trace( "supports gzip: " + supportsGzip );
94
                return supportsGzip;
95
            }
96
        }
97
        return false;
98
    }
99

    
100
    
101

    
102
}