1 package org.apache.archiva.rest.services;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import org.apache.archiva.redback.components.scheduler.CronExpressionValidator;
22 import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
23 import org.apache.archiva.redback.rest.api.services.UtilServices;
24 import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
25 import org.apache.archiva.rest.api.services.CommonServices;
26 import org.apache.commons.io.IOUtils;
27 import org.apache.commons.lang.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30 import org.springframework.stereotype.Service;
32 import javax.annotation.PostConstruct;
33 import javax.inject.Inject;
34 import javax.ws.rs.core.Response;
35 import java.io.ByteArrayInputStream;
36 import java.io.IOException;
37 import java.io.InputStream;
39 import java.util.Properties;
40 import java.util.concurrent.ConcurrentHashMap;
43 * @author Olivier Lamy
45 @Service( "commonServices#rest" )
46 public class DefaultCommonServices
47 implements CommonServices
50 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
52 private Logger log = LoggerFactory.getLogger( getClass() );
55 private UtilServices utilServices;
57 private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
60 protected CronExpressionValidator cronExpressionValidator;
64 throws ArchivaRestServiceException
67 // preload i18n en and fr
68 getAllI18nResources( "en" );
69 getAllI18nResources( "fr" );
72 public String getI18nResources( String locale )
73 throws ArchivaRestServiceException
75 Properties properties = new Properties();
77 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
81 loadResource( properties, resourceName, locale );
84 catch ( IOException e )
86 log.warn( "skip error loading properties {}", resourceName.toString() );
89 return fromProperties( properties );
92 private void loadResource( Properties properties, StringBuilder resourceName, String locale )
96 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
97 // if locale override with locale content
98 if ( StringUtils.isNotEmpty( locale ) )
100 loadResource( properties,
101 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
107 private String fromProperties( final Properties properties )
109 StringBuilder output = new StringBuilder();
111 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
113 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
114 output.append( '\n' );
117 return output.toString();
120 private void loadResource( final Properties finalProperties, String resourceName, String locale )
123 InputStream is = null;
124 Properties properties = new Properties();
127 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
130 properties.load( is );
131 finalProperties.putAll( properties );
135 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
137 log.info( "cannot load resource {}", resourceName );
143 IOUtils.closeQuietly( is );
147 public String getAllI18nResources( String locale )
148 throws ArchivaRestServiceException
151 String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
152 if ( cachedi18n != null )
160 Properties all = utilServices.getI18nProperties( locale );
161 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
162 loadResource( all, resourceName, locale );
164 String i18n = fromProperties( all );
165 cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n );
168 catch ( IOException e )
170 throw new ArchivaRestServiceException( e.getMessage(),
171 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
173 catch ( RedbackServiceException e )
175 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode(), e );
179 private void loadFromString( String propsStr, Properties properties )
180 throws ArchivaRestServiceException
182 InputStream inputStream = null;
185 inputStream = new ByteArrayInputStream( propsStr.getBytes() );
186 properties.load( inputStream );
188 catch ( IOException e )
190 throw new ArchivaRestServiceException( e.getMessage(),
191 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
195 IOUtils.closeQuietly( inputStream );
200 public Boolean validateCronExpression( String cronExpression )
201 throws ArchivaRestServiceException
203 return cronExpressionValidator.validate( cronExpression );