/*
 * Copyright 2016 Bridje Framework.
 *
 * 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 org.bridje.web.impl;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.bridje.http.HttpBridlet;
import org.bridje.http.HttpBridletContext;
import org.bridje.http.HttpBridletRequest;
import org.bridje.http.HttpBridletResponse;
import org.bridje.http.HttpException;
import org.bridje.ioc.Component;
import org.bridje.ioc.InjectNext;
import org.bridje.ioc.Priority;
import org.bridje.vfs.Path;
import org.bridje.vfs.VFile;
import org.bridje.vfs.VFileInputStream;

@Component
@Priority(700)
class RenderFileBridlet implements HttpBridlet
{
    @InjectNext
    private HttpBridlet nextHandler;

    private final Path PUBLIC_PATH = new Path("/web/public");

    @Override
    public boolean handle(HttpBridletContext context) throws IOException, HttpException
    {
        HttpBridletRequest req = context.getRequest();
        String pathStr = req.getPath();
        if(pathStr != null && 
                !(pathStr.endsWith(".view.xml") || pathStr.endsWith(".layout.xml")))
        {
            Path path = new Path(pathStr).getCanonicalPath();
            if(path != null)
            {
                path = PUBLIC_PATH.join(path);
                VFile file = new VFile(path);
                if(file.exists() && file.isFile())
                {
                    HttpBridletResponse resp = context.getResponse();
                    try(InputStream is = new VFileInputStream(file))
                    {
                        try(OutputStream os = resp.getOutputStream())
                        {
                            resp.setContentType(file.getMimeType());
                            copy(is, os);
                            os.flush();
                        }
                    }
                    return true;
                }
            }
        }
        if(nextHandler != null)
        {
            return nextHandler.handle(context);
        }
        return false;
    }

    private void copy(InputStream is, OutputStream os) throws IOException
    {
        byte[] buffer = new byte[1024];
        int bytesCount = is.read(buffer);
        while(bytesCount > -1)
        {
            os.write(buffer, 0, bytesCount);
            bytesCount = is.read(buffer);
        }
    }
}

@@ -1404,32 +1404,118 @@
 Exception%0A    %7B%0A
+        VFile file = context.get(VFile.class);%0A        if(file == null)%0A        %7B%0A    
         HttpBrid
@@ -1561,16 +1561,20 @@
         
+    
 String p
@@ -1601,24 +1601,28 @@
 ();%0A        
+  
+  
 if(pathStr !
@@ -1632,16 +1632,20 @@
 ull && %0A
+    
         
@@ -1721,34 +1721,42 @@
 xml%22)))%0A        
+  
-%7B%0A
+  %7B%0A    
             Path
@@ -1809,24 +1809,28 @@
             
+    
 if(path != n
@@ -1838,34 +1838,41 @@
 ll)%0A            
-%7B%0A
+    %7B%0A   
                 
@@ -1871,16 +1871,17 @@
         
+ 
 path = P
@@ -1911,37 +1911,35 @@
                 
-VFile
+   
  file = new VFil
@@ -1943,32 +1943,105 @@
 File(path);%0A    
+            %7D%0A            %7D%0A        %7D%0A        if(file != null)%0A        %7B%0A
             if(f
@@ -2079,34 +2079,26 @@
             
-    %7B%0A    
+%7B%0A
             
@@ -2163,28 +2163,24 @@
             
- 
-   
 try(InputStr
@@ -2224,36 +2224,32 @@
                 
-    
 %7B%0A              
@@ -2250,28 +2250,24 @@
             
- 
-   
 try(OutputSt
@@ -2312,36 +2312,32 @@
                 
-    
 %7B%0A              
@@ -2346,20 +2346,16 @@
         
- 
-   
 resp.set
@@ -2411,20 +2411,16 @@
         
-    
 copy(is,
@@ -2449,20 +2449,16 @@
         
- 
-   
 os.flush
@@ -2477,32 +2477,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -2495,32 +2495,26 @@
             
-  %7D%0A    
+%7D%0A
             
@@ -2530,34 +2530,16 @@
 n true;%0A
-                %7D%0A
         

Fix in render file bridlet.