001    package org.apache.archiva.web.startup;
002    
003    /*
004     * Licensed to the Apache Software Foundation (ASF) under one
005     * or more contributor license agreements.  See the NOTICE file
006     * distributed with this work for additional information
007     * regarding copyright ownership.  The ASF licenses this file
008     * to you under the Apache License, Version 2.0 (the
009     * "License"); you may not use this file except in compliance
010     * with the License.  You may obtain a copy of the License at
011     *
012     *  http://www.apache.org/licenses/LICENSE-2.0
013     *
014     * Unless required by applicable law or agreed to in writing,
015     * software distributed under the License is distributed on an
016     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017     * KIND, either express or implied.  See the License for the
018     * specific language governing permissions and limitations
019     * under the License.
020     */
021    
022    import org.apache.commons.lang.StringUtils;
023    import org.slf4j.LoggerFactory;
024    
025    import java.util.regex.Matcher;
026    import java.util.regex.Pattern;
027    
028    /**
029     * Banner 
030     *
031     *
032     */
033    public class Banner
034    {
035        private static final String eol = System.getProperty("line.separator");
036    
037        public static String encode( String raw )
038        {
039            // Canonicalize line ends to make them easier to process
040            raw = raw.replace("\r\n", "\n").replace("\r", "\n");
041    
042            StringBuilder encoded = new StringBuilder();
043            int rawlen = raw.length();
044    
045            for ( int i = 0; i < rawlen; i++ )
046            {
047                char c = raw.charAt( i );
048                if ( c == '\\' )
049                {
050                    encoded.append( "$." );
051                }
052                else if ( c == '$' )
053                {
054                    encoded.append( "$$" );
055                }
056                else if ( c == '\n' )
057                {
058                    encoded.append( "$n" );
059                }
060                else if ( Character.isDigit( c ) )
061                {
062                    encoded.append( c );
063                }
064                else if ( Character.isLetter( c ) )
065                {
066                    encoded.append( rot13( c ) );
067                }
068                else if ( i < raw.length() - 1 )
069                {
070                    char nc;
071                    boolean done = false;
072                    int count = 0;
073                    for ( int n = i; !done; n++ )
074                    {
075                        if ( n >= rawlen )
076                        {
077                            break;
078                        }
079    
080                        nc = raw.charAt( n );
081    
082                        if ( nc != c )
083                        {
084                            done = true;
085                        }
086                        else
087                        {
088                            count++;
089                        }
090                    }
091                    if ( count < 3 )
092                    {
093                        encoded.append( c );
094                    }
095                    else
096                    {
097                        encoded.append( "$" ).append( String.valueOf( count ) ).append( c );
098                        i += count - 1;
099                    }
100                }
101                else
102                {
103                    encoded.append( c );
104                }
105            }
106    
107            return encoded.toString();
108        }
109    
110        public static String decode( String encoded )
111        {
112            StringBuilder decoded = new StringBuilder();
113            int enlen = encoded.length();
114            for ( int i = 0; i < enlen; i++ )
115            {
116                char c = encoded.charAt( i );
117                if ( c == '$' )
118                {
119                    char nc = encoded.charAt( i + 1 );
120                    if ( nc == '$' )
121                    {
122                        decoded.append( '$' );
123                        i++;
124                    }
125                    else if ( nc == '.' )
126                    {
127                        decoded.append( '\\' );
128                        i++;
129                    }
130                    else if ( nc == 'n' )
131                    {
132                        decoded.append( eol );
133                        i++;
134                    }
135                    else if ( Character.isDigit( nc ) )
136                    {
137                        int count = 0;
138                        int nn = i + 1;
139                        while ( Character.isDigit( nc ) )
140                        {
141                            count = ( count * 10 );
142                            count += ( nc - '0' );
143                            nc = encoded.charAt( ++nn );
144                        }
145                        for ( int d = 0; d < count; d++ )
146                        {
147                            decoded.append( nc );
148                        }
149                        i = nn;
150                    }
151                }
152                else if ( Character.isLetter( c ) )
153                {
154                    decoded.append( rot13( c ) );
155                }
156                else
157                {
158                    decoded.append( c );
159                }
160            }
161    
162            return decoded.toString();
163        }
164    
165        private static char rot13( char c )
166        {
167            if ( ( c >= 'a' ) && ( c <= 'z' ) )
168            {
169                char dc = c += 13;
170                if ( dc > 'z' )
171                {
172                    dc -= 26;
173                }
174                return dc;
175            }
176            else if ( ( c >= 'A' ) && ( c <= 'Z' ) )
177            {
178                char dc = c += 13;
179                if ( dc > 'Z' )
180                {
181                    dc -= 26;
182                }
183                return dc;
184            }
185            else
186            {
187                return c;
188            }
189        }
190    
191        public static String injectVersion( String text, String version )
192        {
193            Pattern pat = Pattern.compile( "#{2,}" );
194            Matcher mat = pat.matcher( text );
195            StringBuilder ret = new StringBuilder();
196            int off = 0;
197    
198            while ( mat.find( off ) )
199            {
200                ret.append( text.substring( off, mat.start() ) );
201                String repl = mat.group();
202                ret.append( StringUtils.center( version, repl.length() ) );
203                off = mat.end();
204            }
205    
206            ret.append( text.substring( off ) );
207    
208            return ret.toString();
209        }
210    
211        public static String getBanner( String version )
212        {
213            String encodedBanner = "$26 $34_$n$15 /$._$7 /$34 $.$n$14 /`/@),$4 |  Ba" +
214                    " orunys bs nyy bs gur nycnpnf   |$n$14 |  (~'  __| gbvyvat njnl ba " +
215                    "gur Ncnpur Nepuvin |$n$6 _,--.$3_/  |$4 $.$5  cebwrpg grnz, V jbhyq y" +
216                    "vxr gb$3 |$n$4 ,' ,$5 ($3 |$5 $.$5     jrypbzr lbh gb Nepuvin$6 |$" +
217                    "n$4 |  ($6 $.  /$6 |  $32#  |$n$5 $.  )$._/  ,_/$7 |$36 |$n$5 / /$3 " +
218                    "( |/$9 |     uggc://nepuvin.ncnpur.bet/     |$n$4 ( |$4 ( |$10 |     hf" +
219                    "ref@nepuvin.ncnpur.bet$7 |$n$5 $.|$5 $.|$11 $.$34_/$n$n";
220    
221            return injectVersion( decode( encodedBanner ), version );
222        }
223    
224        public static void display( String version )
225        {
226            String banner = getBanner( version );
227            LoggerFactory.getLogger( Banner.class ).info( StringUtils.repeat( "_", 25 ) + eol + banner );
228        }
229    }