/*
 * Copyright 2016 peter.
 *
 * 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 onl.area51.filesystem.http.server;

import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.Objects;
import onl.area51.filesystem.http.PathEntity;
import onl.area51.httpd.HttpAction;
import org.apache.http.HttpStatus;

/**
 * Builds a map of file systems with a specified path prefix. This map can then be used via an HttpAction chain to resolve a path the an nio Path
 * and an appropriate action.
 *
 * @author peter
 */
public interface PathHttpActionBuilder
{

    PathHttpActionBuilder add( PathHttpAction action );

    default PathHttpActionBuilder assertPathExists()
    {
        return add( ( req, resp, context, path ) -> {
            if( path == null || !Files.exists( path, LinkOption.NOFOLLOW_LINKS ) ) {
                HttpAction.sendError( resp, HttpStatus.SC_NOT_FOUND, req.getRequestLine().getUri() );
            }
        } );
    }

    default PathHttpActionBuilder returnPathContent()
    {
        return add( ( req, resp, ctx, path ) -> {
            resp.setStatusCode( HttpStatus.SC_OK );
            PathEntity body = new PathEntity( path );
            resp.setEntity( body );
        } );
    }

    default PathHttpActionBuilder returnPathSizeOnly()
    {
        return add( ( req, resp, ctx, path ) -> {
            resp.setStatusCode( HttpStatus.SC_OK );
            resp.setEntity( new PathEntity.SizeOnly( path ) );
        } );
    }

    PathHttpAction build();

    default HttpAction build( FileSystemMap map )
    {
        PathHttpAction action = build();
        return ( req, resp, ctx ) -> {
            String uri = req.getRequestLine().getUri();
            Path path = map.getPath( uri );
            if( path == null ) {
                HttpAction.sendError( resp, HttpStatus.SC_NOT_FOUND, uri );
            }
            else {
                action.apply( req, resp, ctx, path );
            }
        };
    }

    static PathHttpActionBuilder create()
    {
        return new PathHttpActionBuilder()
        {
            private PathHttpAction action;

            @Override
            public PathHttpActionBuilder add( PathHttpAction action )
            {
                this.action = this.action == null ? action : this.action.andThen( action );
                return this;
            }

            @Override
            public PathHttpAction build()
            {
                Objects.requireNonNull( action, "No actions defined" );
                return action;
            }

        };
    }
}

@@ -716,16 +716,57 @@
 e.Path;%0A
+import java.nio.file.StandardCopyOption;%0A
 import j
@@ -892,21 +892,192 @@
 ttp.Http
-Statu
+Entity;%0Aimport org.apache.http.HttpEntityEnclosingRequest;%0Aimport org.apache.http.HttpStatus;%0Aimport org.apache.http.entity.StringEntity;%0Aimport org.apache.http.util.EntityUtil
 s;%0A%0A/**%0A
@@ -1200,16 +1200,19 @@
 esolve a
+%0A *
  path th
@@ -1224,19 +1224,16 @@
 nio Path
-%0A *
  and an 
@@ -1701,32 +1701,150 @@
     %7D );%0A    %7D%0A%0A
+    /**%0A     * Return the content of a path to the client - specifically a GET request.%0A     *%0A     * @return%0A     */%0A
     default Path
@@ -2095,24 +2095,161 @@
 %7D );%0A    %7D%0A%0A
+    /**%0A     * Returns just the head (i.e. no actual content) to the client - specifically a HEAD request.%0A     *%0A     * @return%0A     */%0A
     default 
@@ -2478,24 +2478,826 @@
 %7D );%0A    %7D%0A%0A
+    /**%0A     * Post the request content to a cache%0A     *%0A     * @return%0A     */%0A    default PathHttpActionBuilder saveContent()%0A    %7B%0A        return add( ( req, resp, ctx, path ) -%3E %7B%0A            if( req instanceof HttpEntityEnclosingRequest ) %7B%0A                HttpEntityEnclosingRequest request = (HttpEntityEnclosingRequest) req;%0A                HttpEntity entity = request.getEntity();%0A                Files.copy( entity.getContent(), path, StandardCopyOption.REPLACE_EXISTING );%0A                resp.setStatusCode( HttpStatus.SC_OK );%0A                resp.setEntity( new StringEntity( %22OK%22 ) );%0A            %7D%0A            else %7B%0A                resp.setStatusCode( HttpStatus.SC_BAD_REQUEST );%0A                resp.setEntity( new StringEntity( %22BAD REQUEST%22 ) );%0A            %7D%0A        %7D );%0A    %7D%0A%0A
     PathHttp

Add initial PUT support