This project has retired. For details please refer to its Attic page.
DefaultRuntimeInfoService xref
View Javadoc
1   package org.apache.archiva.web.api;
2   /*
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   */
20  
21  import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration;
22  import org.apache.archiva.redback.configuration.UserConfigurationKeys;
23  import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
24  import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService;
25  import org.apache.archiva.web.model.CookieInformation;
26  import org.apache.archiva.web.runtime.ArchivaRuntimeInfo;
27  import org.apache.archiva.web.model.ApplicationRuntimeInfo;
28  import org.apache.commons.lang.BooleanUtils;
29  import org.apache.commons.lang.StringUtils;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  import org.springframework.stereotype.Service;
33  
34  import javax.inject.Inject;
35  import javax.servlet.http.HttpServletRequest;
36  import javax.ws.rs.core.Context;
37  import java.text.SimpleDateFormat;
38  import java.util.Date;
39  import java.util.Locale;
40  
41  /**
42   * @author Olivier Lamy
43   */
44  @Service("runtimeInfoService#rest")
45  public class DefaultRuntimeInfoService
46      implements RuntimeInfoService
47  {
48  
49      private Logger i18nLogger = LoggerFactory.getLogger( "archivaMissingi18n.logger" );
50  
51      private ArchivaRuntimeInfo archivaRuntimeInfo;
52  
53      @Inject
54      private RedbackRuntimeConfigurationService redbackRuntimeConfigurationService;
55  
56      @Context
57      protected HttpServletRequest httpServletRequest;
58  
59      @Inject
60      public DefaultRuntimeInfoService( ArchivaRuntimeInfo archivaRuntimeInfo )
61      {
62          this.archivaRuntimeInfo = archivaRuntimeInfo;
63      }
64  
65      @Override
66      public ApplicationRuntimeInfo getApplicationRuntimeInfo( String locale )
67          throws ArchivaRestServiceException
68      {
69          ApplicationRuntimeInfo applicationRuntimeInfo = new ApplicationRuntimeInfo();
70          applicationRuntimeInfo.setBuildNumber( this.archivaRuntimeInfo.getBuildNumber() );
71          applicationRuntimeInfo.setTimestamp( this.archivaRuntimeInfo.getTimestamp() );
72          applicationRuntimeInfo.setVersion( this.archivaRuntimeInfo.getVersion() );
73          applicationRuntimeInfo.setBaseUrl( getBaseUrl( httpServletRequest ) );
74  
75          SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz",
76                                                       new Locale( StringUtils.isEmpty( locale ) ? "en" : locale ) );
77          applicationRuntimeInfo.setTimestampStr( sfd.format( new Date( archivaRuntimeInfo.getTimestamp() ) ) );
78  
79          CookieInformation cookieInformation = new CookieInformation();
80  
81          RedbackRuntimeConfiguration redbackRuntimeConfiguration =
82              redbackRuntimeConfigurationService.getRedbackRuntimeConfiguration();
83  
84          cookieInformation.setDomain(
85              redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_DOMAIN ) );
86          cookieInformation.setPath(
87              redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_PATH ) );
88          cookieInformation.setSecure(
89              redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_SECURE ) );
90          cookieInformation.setTimeout(
91              redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_TIMEOUT ) );
92          cookieInformation.setRememberMeEnabled( BooleanUtils.toBoolean(
93              redbackRuntimeConfiguration.getConfigurationProperties().get(
94                  UserConfigurationKeys.REMEMBER_ME_ENABLED ) ) );
95  
96          applicationRuntimeInfo.setCookieInformation( cookieInformation );
97  
98          return applicationRuntimeInfo;
99      }
100 
101     protected String getBaseUrl( HttpServletRequest req )
102     {
103         return req.getScheme() + "://" + req.getServerName() + ( req.getServerPort() == 80
104             ? ""
105             : ":" + req.getServerPort() ) + req.getContextPath();
106     }
107 
108     @Override
109     public Boolean logMissingI18n( String key )
110     {
111         i18nLogger.info( "missing i18n key : '{}'", key );
112         return Boolean.TRUE;
113     }
114 }