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

com.sun.grizzly.tcp.DynamicContentAdapter Maven / Gradle / Ivy

The newest version!
/*
 * 
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
 * 
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License. You can obtain
 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
 * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
 * Sun designates this particular file as subject to the "Classpath" exception
 * as provided by Sun in the GPL Version 2 section of the License file that
 * accompanied this code.  If applicable, add the following below the License
 * Header, with the fields enclosed by brackets [] replaced by your own
 * identifying information: "Portions Copyrighted [year]
 * [name of copyright owner]"
 * 
 * Contributor(s):
 * 
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 *
 */

package com.sun.grizzly.tcp;


import com.sun.grizzly.util.buf.MessageBytes;
import com.sun.grizzly.util.buf.ByteChunk;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.Date;
import java.util.Locale;
import java.text.SimpleDateFormat;
import java.text.ParseException;

/**
 * Abstract Adapter that contains all the common behaviour of the Adapter implmentation
 * for standalone usage as well as embedded use.
 *
 * @author Jerome Dochez
 * @author Jean-Francois Arcand
 */
abstract public class DynamicContentAdapter extends StaticResourcesAdapter {
    
    private final static int MAX_POST_SIZE = 48 * 1024;
    
    protected static final String RFC_2616_FORMAT = "EEE, d MMM yyyy HH:mm:ss z";
    
    
    protected String contextRoot = null;
    
    
    public DynamicContentAdapter() {
        super();
    }    
    
    public DynamicContentAdapter(String publicDirectory) {
        super(publicDirectory);
    }
    
    
    abstract protected int getTokenID();
    
    
    abstract protected void serviceDynamicContent(Request req, Response res) throws IOException;
    
    
    @Override
    public void afterService(Request req, Response res) throws Exception {
        ; // Let the Adapter handle the life cycle of the request/response.
    }
    
    
    @Override
    public void fireAdapterEvent(String type, Object data) {
        ; // Let the Adapter handle the life cycle of the request/response.
    }
    
    
    private boolean modifiedSince(Request req, File file) {
        try {
            String since = req.getMimeHeaders().getHeader("If-Modified-Since");
            if (since == null) {
                return false;
            }
            
            Date date = new SimpleDateFormat(RFC_2616_FORMAT, Locale.US).parse(since);
            if (date.getTime() > file.lastModified()) {
                return true;
            } else {
                return false;
            }
        } catch (ParseException e) {
            return false;
        }
    }
    
    
    public void service(Request req, Response res) throws Exception {
        MessageBytes mb = req.requestURI();
        ByteChunk requestURI = mb.getByteChunk();
        
        try{
            String uri = requestURI.toString();
            if (contextRoot!=null && requestURI.startsWith(contextRoot)) {
                uri = uri.substring(contextRoot.length());
            }
            File file = new File(getRootFolder(),uri);
            if (file.isDirectory()) {
                uri += "index.html";
                file = new File(file,uri);
            }
            
            if (file.canRead()) {
                super.service(uri, req, res);
                return;
            } else {
                serviceDynamicContent(req, res);
            }
        } catch (Exception e) {
            if (getLogger().isLoggable(Level.SEVERE)) {
                getLogger().log(Level.SEVERE, e.getMessage());
            }
            
            throw e;
        }
    }

    /**
     * Simple InputStream that wrap the Grizzly internal object.
     */
    private class GrizzlyInputStream extends InputStream {
        
        public RequestTupple rt;
        
        public int read() throws IOException {
            return rt.readChunk.substract();
        }
        
        public int read(byte[] b) throws IOException {
            return read(b,0,b.length);
        }
        
        public int read(byte[] b, int off, int len) throws IOException {
            return rt.readChunk.substract(b,off,len);
        }
    }
    
    
    /**
     * Statefull token used to share information with the Containers.
     */
    public class RequestTupple implements ByteChunk.ByteInputChannel{
        
        public ByteChunk readChunk;
        
        public Request req;
        
        public GrizzlyInputStream inputStream;
        
        public RequestTupple(){
            readChunk = new ByteChunk();
            readChunk.setByteInputChannel(this);
            readChunk.setBytes(new byte[MAX_POST_SIZE],0,MAX_POST_SIZE);
            
            inputStream = new GrizzlyInputStream();
            inputStream.rt = this;
        }
        
        public int realReadBytes(byte[] b, int off, int len) throws IOException {
            req.doRead(readChunk);
            return readChunk.substract(b,off,len);
        }
        
        public void recycle(){
            req = null;
            readChunk.recycle();
        }
        
    }
    
    public void setContextRoot(String contextRoot) {
        this.contextRoot = contextRoot;
    }
    
    
    public String getContextRoot() {
        return contextRoot;
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy