All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.webdav.methods.DoGet Maven / Gradle / Ivy

/**
 * Copyright (C) 2006-2017 Apache Software Foundation (https://sourceforge.net/p/webdav-servlet, https://github.com/Commonjava/webdav-handler)
 *
 * Licensed 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 net.sf.webdav.methods;

import static net.sf.webdav.WebdavStatus.SC_METHOD_NOT_ALLOWED;
import static net.sf.webdav.WebdavStatus.SC_NOT_FOUND;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Locale;

import net.sf.webdav.StoredObject;
import net.sf.webdav.exceptions.WebdavException;
import net.sf.webdav.locking.ResourceLocks;
import net.sf.webdav.spi.IMimeTyper;
import net.sf.webdav.spi.ITransaction;
import net.sf.webdav.spi.IWebdavStore;
import net.sf.webdav.spi.WebdavRequest;
import net.sf.webdav.spi.WebdavResponse;

public class DoGet
    extends DoHead
{

    private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger( DoGet.class );

    public DoGet( final IWebdavStore store, final String dftIndexFile, final String insteadOf404, final ResourceLocks resourceLocks,
                  final IMimeTyper mimeTyper, final boolean contentLengthHeader )
    {
        super( store, dftIndexFile, insteadOf404, resourceLocks, mimeTyper, contentLengthHeader );

    }

    @Override
    protected void doBody( final ITransaction transaction, final WebdavResponse resp, final String path )
    {

        try
        {
            final StoredObject so = _store.getStoredObject( transaction, path );
            if ( so.isNullResource() )
            {
                final String methodsAllowed = DeterminableMethod.determineMethodsAllowed( so );
                resp.addHeader( "Allow", methodsAllowed );
                resp.sendError( SC_METHOD_NOT_ALLOWED );
                return;
            }
            final OutputStream out = resp.getOutputStream();
            final InputStream in = _store.getResourceContent( transaction, path );
            try
            {
                int read = -1;
                final byte[] copyBuffer = new byte[BUF_SIZE];

                while ( ( read = in.read( copyBuffer, 0, copyBuffer.length ) ) != -1 )
                {
                    out.write( copyBuffer, 0, read );
                }
            }
            finally
            {
                // flushing causes a IOE if a file is opened on the webserver
                // client disconnected before server finished sending response
                try
                {
                    in.close();
                }
                catch ( final Exception e )
                {
                    LOG.warn( "Closing InputStream causes Exception!\n" + e.toString() );
                }
                try
                {
                    out.flush();
                    out.close();
                }
                catch ( final Exception e )
                {
                    LOG.warn( "Flushing OutputStream causes Exception!\n" + e.toString() );
                }
            }
        }
        catch ( final Exception e )
        {
            LOG.trace( e.toString() );
        }
    }

    @Override
    protected void folderBody( final ITransaction transaction, final String path, final WebdavResponse resp, final WebdavRequest req )
        throws IOException, WebdavException
    {

        final StoredObject so = _store.getStoredObject( transaction, path );
        if ( so == null )
        {
            resp.sendError( SC_NOT_FOUND, req.getRequestURI() );
        }
        else
        {

            if ( so.isNullResource() )
            {
                final String methodsAllowed = DeterminableMethod.determineMethodsAllowed( so );
                resp.addHeader( "Allow", methodsAllowed );
                resp.sendError( SC_METHOD_NOT_ALLOWED );
                return;
            }

            if ( so.isFolder() )
            {
                // TODO some folder response (for browsers, DAV tools
                // use propfind) in html?
                final DateFormat shortDF = getDateTimeFormat( req.getLocale() );
                resp.setContentType( "text/html" );
                resp.setCharacterEncoding( "UTF8" );
                final OutputStream out = resp.getOutputStream();
                String[] children = _store.getChildrenNames( transaction, path );
                children = children == null ? new String[] {} : children;

                // FIXME Use a content template for this!!
                final StringBuilder childrenTemp = new StringBuilder();
                childrenTemp.append( "Content of folder" );
                childrenTemp.append( path );
                childrenTemp.append( "" );
                childrenTemp.append( "" );
                childrenTemp.append( getHeader( transaction, path, resp, req ) );
                childrenTemp.append( "" );
                childrenTemp.append( "" );
                childrenTemp.append( "" );
                childrenTemp.append( "" );
                boolean isEven = false;
                for ( final String child : children )
                {
                    isEven = !isEven;
                    childrenTemp.append( "" );
                    childrenTemp.append( "" );
                    if ( obj.isFolder() )
                    {
                        childrenTemp.append( "" );
                    }
                    else
                    {
                        childrenTemp.append( "" );
                    }
                    if ( obj.getCreationDate() != null )
                    {
                        childrenTemp.append( "" );
                    }
                    else
                    {
                        childrenTemp.append( "" );
                    }
                    if ( obj.getLastModified() != null )
                    {
                        childrenTemp.append( "" );
                    }
                    else
                    {
                        childrenTemp.append( "" );
                    }
                    childrenTemp.append( "" );
                }
                childrenTemp.append( "
NameSizeCreatedModified
Parent
" ); childrenTemp.append( "" ); childrenTemp.append( child ); childrenTemp.append( "Folder" ); childrenTemp.append( obj.getResourceLength() ); childrenTemp.append( " Bytes" ); childrenTemp.append( shortDF.format( obj.getCreationDate() ) ); childrenTemp.append( "" ); childrenTemp.append( shortDF.format( obj.getLastModified() ) ); childrenTemp.append( "
" ); childrenTemp.append( getFooter( transaction, path, resp, req ) ); childrenTemp.append( "" ); out.write( childrenTemp.toString() .getBytes( "UTF-8" ) ); } } } /** * Return the CSS styles used to display the HTML representation * of the webdav content. * * @return String returning the CSS style sheet used to display result in html format */ protected String getCSS() { // The default styles to use String retVal = "body {\n" + " font-family: Arial, Helvetica, sans-serif;\n" + "}\n" + "h1 {\n" + " font-size: 1.5em;\n" + "}\n" + "th {\n" + " background-color: #9DACBF;\n" + "}\n" + "table {\n" + " border-top-style: solid;\n" + " border-right-style: solid;\n" + " border-bottom-style: solid;\n" + " border-left-style: solid;\n" + "}\n" + "td {\n" + " margin: 0px;\n" + " padding-top: 2px;\n" + " padding-right: 5px;\n" + " padding-bottom: 2px;\n" + " padding-left: 5px;\n" + "}\n" + "tr.even {\n" + " background-color: #CCCCCC;\n" + "}\n" + "tr.odd {\n" + " background-color: #FFFFFF;\n" + "}\n" + ""; try { // Try loading one via class loader and use that one instead final ClassLoader cl = getClass().getClassLoader(); final InputStream iStream = cl.getResourceAsStream( "webdav.css" ); if ( iStream != null ) { // Found css via class loader, use that one final StringBuilder out = new StringBuilder(); final byte[] b = new byte[4096]; for ( int n; ( n = iStream.read( b ) ) != -1; ) { out.append( new String( b, 0, n ) ); } retVal = out.toString(); } } catch ( final Exception ex ) { LOG.error( "Error in reading webdav.css", ex ); } return retVal; } /** * Return this as the Date/Time format for displaying Creation + Modification dates * * @param browserLocale * @return DateFormat used to display creation and modification dates */ protected DateFormat getDateTimeFormat( final Locale browserLocale ) { return SimpleDateFormat.getDateTimeInstance( SimpleDateFormat.SHORT, SimpleDateFormat.MEDIUM, browserLocale ); } /** * Return the header to be displayed in front of the folder content * * @param transaction * @param path * @param resp * @param req * @return String representing the header to be display in front of the folder content */ protected String getHeader( final ITransaction transaction, final String path, final WebdavResponse resp, final WebdavRequest req ) { return "

Content of folder " + path + "

"; } /** * Return the footer to be displayed after the folder content * * @param transaction * @param path * @param resp * @param req * @return String representing the footer to be displayed after the folder content */ protected String getFooter( final ITransaction transaction, final String path, final WebdavResponse resp, final WebdavRequest req ) { return ""; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy