001package org.apache.archiva.web.api; 002/* 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, 014 * software distributed under the License is distributed on an 015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 016 * KIND, either express or implied. See the License for the 017 * specific language governing permissions and limitations 018 * under the License. 019 */ 020 021import org.apache.archiva.admin.model.beans.RedbackRuntimeConfiguration; 022import org.apache.archiva.redback.configuration.UserConfigurationKeys; 023import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 024import org.apache.archiva.rest.api.services.RedbackRuntimeConfigurationService; 025import org.apache.archiva.web.model.CookieInformation; 026import org.apache.archiva.web.runtime.ArchivaRuntimeInfo; 027import org.apache.archiva.web.model.ApplicationRuntimeInfo; 028import org.apache.commons.lang.BooleanUtils; 029import org.apache.commons.lang.StringUtils; 030import org.slf4j.Logger; 031import org.slf4j.LoggerFactory; 032import org.springframework.stereotype.Service; 033 034import javax.inject.Inject; 035import javax.servlet.http.HttpServletRequest; 036import javax.ws.rs.core.Context; 037import java.text.SimpleDateFormat; 038import java.util.Date; 039import java.util.Locale; 040 041/** 042 * @author Olivier Lamy 043 */ 044@Service("runtimeInfoService#rest") 045public class DefaultRuntimeInfoService 046 implements RuntimeInfoService 047{ 048 049 private Logger i18nLogger = LoggerFactory.getLogger( "archivaMissingi18n.logger" ); 050 051 private ArchivaRuntimeInfo archivaRuntimeInfo; 052 053 @Inject 054 private RedbackRuntimeConfigurationService redbackRuntimeConfigurationService; 055 056 @Context 057 protected HttpServletRequest httpServletRequest; 058 059 @Inject 060 public DefaultRuntimeInfoService( ArchivaRuntimeInfo archivaRuntimeInfo ) 061 { 062 this.archivaRuntimeInfo = archivaRuntimeInfo; 063 } 064 065 @Override 066 public ApplicationRuntimeInfo getApplicationRuntimeInfo( String locale ) 067 throws ArchivaRestServiceException 068 { 069 ApplicationRuntimeInfo applicationRuntimeInfo = new ApplicationRuntimeInfo(); 070 applicationRuntimeInfo.setBuildNumber( this.archivaRuntimeInfo.getBuildNumber() ); 071 applicationRuntimeInfo.setTimestamp( this.archivaRuntimeInfo.getTimestamp() ); 072 applicationRuntimeInfo.setVersion( this.archivaRuntimeInfo.getVersion() ); 073 applicationRuntimeInfo.setBaseUrl( getBaseUrl( httpServletRequest ) ); 074 075 SimpleDateFormat sfd = new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ssz", 076 new Locale( StringUtils.isEmpty( locale ) ? "en" : locale ) ); 077 applicationRuntimeInfo.setTimestampStr( sfd.format( new Date( archivaRuntimeInfo.getTimestamp() ) ) ); 078 079 CookieInformation cookieInformation = new CookieInformation(); 080 081 RedbackRuntimeConfiguration redbackRuntimeConfiguration = 082 redbackRuntimeConfigurationService.getRedbackRuntimeConfiguration(); 083 084 cookieInformation.setDomain( 085 redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_DOMAIN ) ); 086 cookieInformation.setPath( 087 redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_PATH ) ); 088 cookieInformation.setSecure( 089 redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_SECURE ) ); 090 cookieInformation.setTimeout( 091 redbackRuntimeConfiguration.getConfigurationProperties().get( UserConfigurationKeys.REMEMBER_ME_TIMEOUT ) ); 092 cookieInformation.setRememberMeEnabled( BooleanUtils.toBoolean( 093 redbackRuntimeConfiguration.getConfigurationProperties().get( 094 UserConfigurationKeys.REMEMBER_ME_ENABLED ) ) ); 095 096 applicationRuntimeInfo.setCookieInformation( cookieInformation ); 097 098 return applicationRuntimeInfo; 099 } 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}