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

org.archive.crawler.restlet.Flash Maven / Gradle / Ivy

The newest version!
/*
 *  This file is part of the Heritrix web crawler (crawler.archive.org).
 *
 *  Licensed to the Internet Archive (IA) by one or more individual 
 *  contributors. 
 *
 *  The IA licenses this file to You 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.archive.crawler.restlet;

import java.io.PrintWriter;
import java.io.Writer;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Cookie;
import org.restlet.data.CookieSetting;
import org.restlet.util.Series;

/**
 * Utility for including a brief last-action or background-action 
 * message on web responses. 
 * 
 * @author gojomo
 *
 */
public class Flash {
    /** usual types */
    public enum Kind {ACK, NACK, ADVISORY}
    
    protected static long nextdrop = RandomUtils.nextLong();
    protected static Map dropboxes = new LinkedHashMap() {
        private static final long serialVersionUID = 1L;
        @Override
        protected boolean removeEldestEntry(Entry eldest) {
            return size()>100;
        }
        
    };
    
    public static void addFlash(Response response, String message) {
        addFlash(response, message, Kind.ACK);
    }
    
    public static void addFlash(Response response, String message, Kind kind) {
        dropboxes.put(nextdrop,new Flash(message, kind));
        Series cookies = response.getCookieSettings();
        CookieSetting flashdrop = null; 
        for(CookieSetting cs : cookies) {
            if(cs.getName().equals("flashdrop")) {
                flashdrop = cs; 
            }
        }
        if(flashdrop == null) {
            cookies.add(new CookieSetting("flashdrop",Long.toString(nextdrop)));
        } else {
            flashdrop.setValue(flashdrop.getValue()+","+Long.toString(nextdrop));
        }
        nextdrop++;
    }
    
    public static List getFlashes(Request request) {
        List flashes = new LinkedList();
        Series cookies = request.getCookies();
        String flashdrops = cookies.getFirstValue("flashdrop");
        if (StringUtils.isBlank(flashdrops)) {
            return flashes;
        }
        for (String dropbox : flashdrops.split(",")) {
            if(dropbox!=null) {
                Flash flash = dropboxes.remove(Long.parseLong(dropbox));
                if(flash!=null) {
                    flashes.add(flash); 
                }
            }
        }
        return flashes;
    }
    
    public static void renderFlashesHTML(Writer writer, Request request) {
        PrintWriter pw = new PrintWriter(writer); 
        for(Flash flash : getFlashes(request)) {
            pw.println("
"); pw.println(flash.getMessage()); pw.println("
"); } pw.flush(); } /** kind of flash, ACK NACK or ADVISORY */ protected Kind kind; /** the message to show, if any */ protected String message; /** * Create an ACK flash of default styling with the given message. * * @param message */ public Flash(String message) { this(message, Kind.ACK); } /** * Create a Flash of the given kind, message with default styling. * * @param kind * @param message */ public Flash(String message, Kind kind) { this.kind = kind; this.message = message; } /** * Indicate whether the Flash should persist. The usual and * default case is that a Flash displays once and then expires. * * @return boolean whether to discard Flash */ public boolean isExpired() { return true; } public String getMessage() { return this.message; } public Kind getKind() { return this.kind; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy