001    /*
002     * Created by IntelliJ IDEA.
003     * User: d@hd.org
004     * Date: 22-Jul-02
005     * Time: 18:13:44
006    
007    Copyright (c) 1996-2011, Damon Hart-Davis
008    All rights reserved.
009    
010    Redistribution and use in source and binary forms, with or without
011    modification, are permitted provided that the following conditions are
012    met:
013    
014      * Redistributions of source code must retain the above copyright
015        notice, this list of conditions and the following disclaimer.
016    
017      * Redistributions in binary form must reproduce the above copyright
018        notice, this list of conditions and the following disclaimer in the
019        documentation and/or other materials provided with the
020        distribution.
021    
022    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
023    IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
024    TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
025    PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
026    OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
027    SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
028    LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
029    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
030    THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031    (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
032    OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033    
034     */
035    package org.hd.d.pg2k.webSvr.ads;
036    
037    import java.io.IOException;
038    import java.net.URLEncoder;
039    import java.util.Random;
040    
041    import org.hd.d.pg2k.svrCore.CoreConsts;
042    import org.hd.d.pg2k.svrCore.ExhibitName;
043    import org.hd.d.pg2k.webSvr.util.WebUtils;
044    
045    /**This implements the e-shirt advertisement bean.
046     * This is mainly here as an example of how an AdBean might work;
047     * in general AdBean classes should be made available at run-time
048     * to the server(s) by being placed in their class-path or by
049     * some other mechanism.
050     */
051    public final class EShirtAdBean implements AdBeanInterface
052        {
053        /**If non-null, overrides normal body text.
054         * Accessed under the instance lock.
055         */
056        private String overrideBodyText;
057    
058        /**Can be used to set general parameters for the bean, or can be null.
059         * If supplied and is non-null, this overrides the body copy supplied
060         * in the bean within the link, though we feel at liberty to trim
061         * whitespace.
062         */
063        public synchronized AdBeanInterface setParams(String rawParams)
064            {
065            if(rawParams != null) { rawParams = rawParams.trim(); }
066    
067            overrideBodyText = rawParams;
068    
069            return(this);
070            }
071    
072        /**Can make an HTML body-text insert in the given context.
073         * We can only do this if the full exhibit name is passed to us
074         * and if the file is of a suitable type (a .jpg or a .gif)
075         * (so we will perform various simple checks).
076         */
077        public boolean canMakeHTMLBodyInsert(final AdBeanInterface.Context ctxt)
078            {
079            final String exhibit = ctxt.getExhibit();
080            if(exhibit == null) { return(false); } // No exhibit supplied.
081    
082            // A full exhibit name must contain at least a '/'.
083            if(exhibit.indexOf('/') == -1) { return(false); } // Bad name...
084    
085            // We only support certain exhibit types for e-shirt,
086            // so just check for the right extensions here.
087            if(!exhibit.endsWith(".jpg") &&
088               !exhibit.endsWith(".gif"))
089                { return(false); }
090    
091            // Because we can (we are being compiled in with PG2K itself),
092            // we check that the name syntax is valid,
093            // but an externally-compiled bean would not do this
094            // (it would not be able to).
095            if(!ExhibitName.validNameSyntax(exhibit))
096                { return(false); }
097    
098            // Everything seems OK.
099            return(true);
100            }
101    
102        /**Make HTML body text in the given context, or null if none can be made.
103         * This takes a random-number source and the user's locale
104         * and generates a segment of HTML to insert into a page.
105         * <p>
106         * We should not be called if the context is wrong, but we double-check
107         * and return "" if we know we can't generate a sensible HTML insert,
108         * else we blindly accept the exhibit name we are given in the context.
109         */
110        public synchronized String getHTMLBodyInsert(final AdBeanInterface.Context ctxt,
111                                                     final Random source)
112            {
113            if(!canMakeHTMLBodyInsert(ctxt)) { return(""); } // Definitely cannot.
114    
115            // Insert the exhibit link as an (UTF-8) encoded URL.
116            // We reach into PG2K utility classes to help with this,
117            // but in general an AdBean could not and need not do this.
118            //
119            // If we encounter an error then we return ""...
120            try
121                {
122                return((new StringBuilder(512)).
123                    append("<img src=\"/_static/_3rdPartyImg/EShirt.gif\" width=150 height=58 align=middle> ").
124                    append("<a target=\"_top\" ").
125                    append("href=\"http://www.eshirt.it/carrello/gt_urlimg.php?urluserfile=").
126                    append(URLEncoder.encode(WebUtils.makeExhibitURL(ctxt.getExhibit(), null, null).toString(), CoreConsts.FILE_ENCODING_UTF_8)).
127                    append("&max=1&newusrid=3d2308ed663ce&").
128                    append("newfrom=3d2308ed663ce&").
129                    append("prj_source=0207246D40C5\">").
130                    append((overrideBodyText == null) ? "Print this on a T-shirt!" : overrideBodyText).
131                    append("</a>").toString());
132                }
133            catch(final IOException e)
134                {
135                e.printStackTrace();
136                return(""); // Whoops!
137                }
138            }
139        }