View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.continuum.web.appareance;
20  
21  import java.io.File;
22  import java.io.FileWriter;
23  import java.io.IOException;
24  import java.util.Calendar;
25  
26  import org.apache.continuum.web.appearance.ContinuumAppearance;
27  import org.apache.continuum.web.appearance.io.xpp3.ContinuumAppearanceModelsXpp3Reader;
28  import org.apache.continuum.web.appearance.io.xpp3.ContinuumAppearanceModelsXpp3Writer;
29  import org.codehaus.plexus.personality.plexus.lifecycle.phase.Initializable;
30  import org.codehaus.plexus.personality.plexus.lifecycle.phase.InitializationException;
31  import org.codehaus.plexus.util.ReaderFactory;
32  import org.codehaus.plexus.util.StringUtils;
33  import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
34  import org.jsoup.Jsoup;
35  import org.jsoup.safety.Whitelist;
36  import org.slf4j.Logger;
37  import org.slf4j.LoggerFactory;
38  
39  /**
40   * @author <a href="mailto:olamy@apache.org">olamy</a>
41   * @version $Id: DefaultAppareanceConfiguration.java 1101669 2011-05-10 22:46:21Z ctan $
42   * @plexus.component role="org.apache.maven.continuum.web.appareance.AppareanceConfiguration" role-hint="default"
43   * @since 10 nov. 07
44   */
45  public class DefaultAppareanceConfiguration
46      implements AppareanceConfiguration, Initializable
47  {
48      private static final Logger log = LoggerFactory.getLogger( DefaultAppareanceConfiguration.class );
49  
50      private String footer;
51  
52      public static final String APPEARANCE_FILE_NAME = "continuum-appearance.xml";
53  
54      private ContinuumAppearance continuumAppearance = new ContinuumAppearance();
55  
56      // ------------------------------------------------
57      //  Plexus Lifecycle
58      // ------------------------------------------------
59  
60      public void initialize()
61          throws InitializationException
62      {
63  
64          File appearanceConfFile = getAppearanceConfigurationFile();
65  
66          if ( appearanceConfFile.exists() )
67          {
68              try
69              {
70                  ContinuumAppearanceModelsXpp3Reader appearanceReader = new ContinuumAppearanceModelsXpp3Reader();
71                  this.continuumAppearance = appearanceReader.read( ReaderFactory.newXmlReader( appearanceConfFile ) );
72                  if ( continuumAppearance != null )
73                  {
74                      this.footer = continuumAppearance.getFooter();
75                  }
76              }
77              catch ( IOException e )
78              {
79                  log.warn(
80                      "skip IOException reading appearance file " + APPEARANCE_FILE_NAME + ", msg " + e.getMessage() );
81              }
82              catch ( XmlPullParserException e )
83              {
84                  log.warn( "skip XmlPullParserException reading appearance file " + APPEARANCE_FILE_NAME + ", msg " +
85                      e.getMessage() );
86              }
87          }
88          if ( StringUtils.isEmpty( this.footer ) )
89          {
90              // initiate with default footer (save in registry ?)
91              this.footer = getDefaultFooter();
92          }
93      }
94  
95      /**
96       * @see org.apache.maven.continuum.web.appareance.AppareanceConfiguration#getFooter()
97       */
98      public String getFooter()
99      {
100         return this.footer;
101     }
102 
103     /**
104      * @see org.apache.maven.continuum.web.appareance.AppareanceConfiguration#saveFooter(java.lang.String)
105      */
106     public void saveFooter( String footerHtmlContent )
107         throws IOException
108     {
109         String safeFooterHtmlContent = Jsoup.clean( footerHtmlContent, Whitelist.basic() );
110 
111         continuumAppearance.setFooter( safeFooterHtmlContent );
112         ContinuumAppearanceModelsXpp3Writer writer = new ContinuumAppearanceModelsXpp3Writer();
113         File confFile = getAppearanceConfigurationFile();
114         if ( !confFile.exists() )
115         {
116             confFile.getParentFile().mkdirs();
117         }
118         FileWriter fileWriter = new FileWriter( confFile );
119         writer.write( fileWriter, continuumAppearance );
120         fileWriter.close();
121         this.footer = safeFooterHtmlContent;
122     }
123 
124 
125     private String getDefaultFooter()
126     {
127         int inceptionYear = 2005;
128         int currentYear = Calendar.getInstance().get( Calendar.YEAR );
129         StringBuilder stringBuilder = new StringBuilder();
130         stringBuilder.append( "<div class=\"xright\">" );
131         stringBuilder.append( "Copyright &copy; " );
132         stringBuilder.append( String.valueOf( inceptionYear ) ).append( "-" ).append( String.valueOf( currentYear ) );
133         stringBuilder.append( "&nbsp;The Apache Software Foundation" );
134         stringBuilder.append( "</div> <div class=\"clear\"><hr/></div>" );
135         return stringBuilder.toString();
136     }
137 
138 
139     private File getAppearanceConfigurationFile()
140     {
141         return new File(
142             System.getProperty( "appserver.base" ) + File.separator + "conf" + File.separator + APPEARANCE_FILE_NAME );
143     }
144 }