001/*
002 *  Copyright 2016 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.core.resources;
017
018import java.io.IOException;
019import java.io.InputStream;
020import java.io.OutputStream;
021import java.io.Serializable;
022
023import org.apache.cocoon.ProcessingException;
024import org.apache.commons.io.IOUtils;
025
026/**
027 * Default resource handler. It accepts range requests and is able to return its length.
028 */
029public class DefaultResourceHandler extends SimpleResourceHandler
030{
031    public boolean acceptRanges()
032    {
033        return true;
034    }
035    
036    @Override
037    public Serializable getKey()
038    {
039        // don't cache potentially very big resources (video, documents, ...)
040        return null;
041    }
042    
043    public void generate(OutputStream out, long offset, long length) throws IOException, ProcessingException
044    {
045        try (InputStream is = _source.getInputStream())
046        {
047            IOUtils.copyLarge(is, out, offset, length);
048        }
049    }
050
051    @Override
052    public long getLength()
053    {
054        return _source.getContentLength();
055    }
056}