package com.ettrema.json;

import com.bradmcevoy.common.Path;
import com.bradmcevoy.http.GetableResource;
import com.bradmcevoy.http.Resource;
import com.bradmcevoy.http.ResourceFactory;
import com.bradmcevoy.http.exceptions.BadRequestException;
import com.bradmcevoy.http.exceptions.NotAuthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author brad
 */
public class AjaxLoginResourceFactory implements ResourceFactory{

    private static final Logger log = LoggerFactory.getLogger( AjaxLoginResourceFactory.class );

    private final String suffix;
    private final ResourceFactory wrapped;

    public AjaxLoginResourceFactory( String suffix, ResourceFactory wrapped ) {
        this.suffix = suffix;
        this.wrapped = wrapped;
    }

    @Override
    public Resource getResource( String host, String path ) throws NotAuthorizedException, BadRequestException {
        if(path.endsWith( suffix )) {
            int i = path.lastIndexOf( suffix);
            String p2 = path.substring( 0, i);
            Resource r = wrapped.getResource( host, p2);
            if( r != null) {
                if( r instanceof GetableResource) {
                    GetableResource gr = (GetableResource) r;
                    Path pathFull = Path.path( path );
                    log.debug( "found an ajax resource, wrapping a: " + gr.getClass());
                    return new AjaxLoginResource( pathFull.getName(), gr );
                } else {
                    return r;
                }
            }
        }
        return wrapped.getResource( host, path );
    }
}

@@ -563,22 +563,16 @@
 private 
-final 
 String s
@@ -572,24 +572,35 @@
 tring suffix
+ = %22.login%22
 ;%0A    privat
@@ -633,16 +633,128 @@
 apped;%0A%0A
+    public AjaxLoginResourceFactory( ResourceFactory wrapped ) %7B%0A        this.wrapped = wrapped;%0A    %7D    %0A    %0A
     publ
@@ -1724,11 +1724,163 @@
 );%0A    %7D
+%0A%0A    public String getSuffix() %7B%0A        return suffix;%0A    %7D%0A%0A    public void setSuffix(String suffix) %7B%0A        this.suffix = suffix;%0A    %7D%0A    %0A    
 %0A%7D%0A

add default suffix for simpler integration