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.lang.StringUtils;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Service;
31 import javax.annotation.PostConstruct;
32 import javax.inject.Inject;
33 import javax.ws.rs.core.Response;
34 import java.io.ByteArrayInputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
38 import java.util.Properties;
39 import java.util.concurrent.ConcurrentHashMap;
42 * @author Olivier Lamy
44 @Service("commonServices#rest")
45 public class DefaultCommonServices
46 implements CommonServices
49 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
51 private Logger log = LoggerFactory.getLogger( getClass() );
54 private UtilServices utilServices;
56 private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
59 protected CronExpressionValidator cronExpressionValidator;
63 throws ArchivaRestServiceException
66 // preload i18n en and fr
67 getAllI18nResources( "en" );
68 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 Properties properties = new Properties();
124 try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ))
128 properties.load( is );
129 finalProperties.putAll( properties );
133 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
135 log.info( "cannot load resource {}", resourceName );
142 public String getAllI18nResources( String locale )
143 throws ArchivaRestServiceException
146 String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
147 if ( cachedi18n != null )
155 Properties all = utilServices.getI18nProperties( locale );
156 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
157 loadResource( all, resourceName, locale );
159 String i18n = fromProperties( all );
160 cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n );
163 catch ( IOException e )
165 throw new ArchivaRestServiceException( e.getMessage(),
166 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
168 catch ( RedbackServiceException e )
170 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode(), e );
174 private void loadFromString( String propsStr, Properties properties )
175 throws ArchivaRestServiceException
177 try (InputStream inputStream = new ByteArrayInputStream( propsStr.getBytes() ))
179 properties.load( inputStream );
181 catch ( IOException e )
183 throw new ArchivaRestServiceException( e.getMessage(),
184 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
190 public Boolean validateCronExpression( String cronExpression )
191 throws ArchivaRestServiceException
193 return cronExpressionValidator.validate( cronExpression );