001/*
002 *  Copyright 2018 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.workspaces.dav;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.time.ZoneId;
021import java.time.format.DateTimeFormatter;
022import java.util.TimeZone;
023
024import org.apache.cocoon.ProcessingException;
025import org.apache.cocoon.environment.ObjectModelHelper;
026import org.apache.cocoon.environment.Request;
027import org.apache.cocoon.environment.Response;
028import org.apache.cocoon.environment.http.HttpResponse;
029import org.apache.cocoon.reading.ServiceableReader;
030import org.apache.commons.compress.utils.IOUtils;
031import org.xml.sax.SAXException;
032
033import org.ametys.core.util.DateUtils;
034import org.ametys.plugins.explorer.resources.Resource;
035import org.ametys.plugins.repository.AmetysObject;
036
037/**
038 * Reader for Webdav GET, honouring ETag and If-None-Match headers.
039 */
040public class WebdavResourceReader extends ServiceableReader
041{
042    public void generate() throws IOException, SAXException, ProcessingException
043    {
044        Request request = ObjectModelHelper.getRequest(objectModel);
045        AmetysObject object = (AmetysObject) request.getAttribute("resource");
046        
047        Response response = ObjectModelHelper.getResponse(objectModel);
048
049        if (object instanceof Resource)
050        {
051            Resource resource = (Resource) object;
052            response.setHeader("Content-Type", resource.getMimeType());
053            
054            String lastModified = DateTimeFormatter.RFC_1123_DATE_TIME.withZone(TimeZone.getTimeZone("GMT").toZoneId()).format(DateUtils.asZonedDateTime(resource.getLastModified(), ZoneId.systemDefault()));
055            response.setHeader("Last-Modified", lastModified);
056            response.setHeader("ETag", resource.getLastModified().toString());
057            
058            String etag = request.getHeader("If-None-Match");
059            if (etag != null && etag.equals(resource.getLastModified().toString()))
060            {
061                ((HttpResponse) response).setStatus(304);
062                response.setHeader("Content-Length", "0");
063                return;
064            }
065            
066            try (InputStream is = resource.getInputStream())
067            {
068                response.setHeader("Content-Length", String.valueOf(resource.getLength()));
069                IOUtils.copy(is, out);
070            }
071        }
072    }
073}