001 package org.apache.archiva.rest.services; 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 021 import org.apache.archiva.redback.components.scheduler.CronExpressionValidator; 022 import org.apache.archiva.redback.rest.api.services.RedbackServiceException; 023 import org.apache.archiva.redback.rest.api.services.UtilServices; 024 import org.apache.archiva.rest.api.services.ArchivaRestServiceException; 025 import org.apache.archiva.rest.api.services.CommonServices; 026 import org.apache.commons.io.IOUtils; 027 import org.apache.commons.lang.StringUtils; 028 import org.slf4j.Logger; 029 import org.slf4j.LoggerFactory; 030 import org.springframework.stereotype.Service; 031 032 import javax.annotation.PostConstruct; 033 import javax.inject.Inject; 034 import javax.ws.rs.core.Response; 035 import java.io.ByteArrayInputStream; 036 import java.io.IOException; 037 import java.io.InputStream; 038 import java.util.Map; 039 import java.util.Properties; 040 import java.util.concurrent.ConcurrentHashMap; 041 042 /** 043 * @author Olivier Lamy 044 */ 045 @Service( "commonServices#rest" ) 046 public class DefaultCommonServices 047 implements CommonServices 048 { 049 050 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default"; 051 052 private Logger log = LoggerFactory.getLogger( getClass() ); 053 054 @Inject 055 private UtilServices utilServices; 056 057 private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>(); 058 059 @Inject 060 protected CronExpressionValidator cronExpressionValidator; 061 062 @PostConstruct 063 public void init() 064 throws ArchivaRestServiceException 065 { 066 067 // preload i18n en and fr 068 getAllI18nResources( "en" ); 069 getAllI18nResources( "fr" ); 070 } 071 072 public String getI18nResources( String locale ) 073 throws ArchivaRestServiceException 074 { 075 Properties properties = new Properties(); 076 077 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME ); 078 try 079 { 080 081 loadResource( properties, resourceName, locale ); 082 083 } 084 catch ( IOException e ) 085 { 086 log.warn( "skip error loading properties {}", resourceName.toString() ); 087 } 088 089 return fromProperties( properties ); 090 } 091 092 private void loadResource( Properties properties, StringBuilder resourceName, String locale ) 093 throws IOException 094 { 095 // load default 096 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale ); 097 // if locale override with locale content 098 if ( StringUtils.isNotEmpty( locale ) ) 099 { 100 loadResource( properties, 101 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(), 102 locale ); 103 } 104 105 } 106 107 private String fromProperties( final Properties properties ) 108 { 109 StringBuilder output = new StringBuilder(); 110 111 for ( Map.Entry<Object, Object> entry : properties.entrySet() ) 112 { 113 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() ); 114 output.append( '\n' ); 115 } 116 117 return output.toString(); 118 } 119 120 private void loadResource( final Properties finalProperties, String resourceName, String locale ) 121 throws IOException 122 { 123 InputStream is = null; 124 Properties properties = new Properties(); 125 try 126 { 127 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ); 128 if ( is != null ) 129 { 130 properties.load( is ); 131 finalProperties.putAll( properties ); 132 } 133 else 134 { 135 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) ) 136 { 137 log.info( "cannot load resource {}", resourceName ); 138 } 139 } 140 } 141 finally 142 { 143 IOUtils.closeQuietly( is ); 144 } 145 } 146 147 public String getAllI18nResources( String locale ) 148 throws ArchivaRestServiceException 149 { 150 151 String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) ); 152 if ( cachedi18n != null ) 153 { 154 return cachedi18n; 155 } 156 157 try 158 { 159 160 Properties all = utilServices.getI18nProperties( locale ); 161 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME ); 162 loadResource( all, resourceName, locale ); 163 164 String i18n = fromProperties( all ); 165 cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n ); 166 return i18n; 167 } 168 catch ( IOException e ) 169 { 170 throw new ArchivaRestServiceException( e.getMessage(), 171 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); 172 } 173 catch ( RedbackServiceException e ) 174 { 175 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode(), e ); 176 } 177 } 178 179 private void loadFromString( String propsStr, Properties properties ) 180 throws ArchivaRestServiceException 181 { 182 InputStream inputStream = null; 183 try 184 { 185 inputStream = new ByteArrayInputStream( propsStr.getBytes() ); 186 properties.load( inputStream ); 187 } 188 catch ( IOException e ) 189 { 190 throw new ArchivaRestServiceException( e.getMessage(), 191 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e ); 192 } 193 finally 194 { 195 IOUtils.closeQuietly( inputStream ); 196 } 197 } 198 199 200 public Boolean validateCronExpression( String cronExpression ) 201 throws ArchivaRestServiceException 202 { 203 return cronExpressionValidator.validate( cronExpression ); 204 } 205 }