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.rest.api.services.ArchivaRestServiceException;
22 import org.apache.archiva.rest.api.services.CommonServices;
23 import org.apache.commons.io.IOUtils;
24 import org.apache.commons.lang.StringUtils;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.stereotype.Service;
29 import java.io.IOException;
30 import java.io.InputStream;
32 import java.util.Properties;
35 * @author Olivier Lamy
37 @Service( "commonServices#rest" )
38 public class DefaultCommonServices
39 implements CommonServices
42 private Logger log = LoggerFactory.getLogger( getClass() );
44 public String getI18nResources( String locale )
45 throws ArchivaRestServiceException
47 Properties properties = new Properties();
49 StringBuilder resourceName = new StringBuilder( "org/apache/archiva/i18n/default" );
53 loadResource( properties, resourceName, locale );
56 catch ( IOException e )
58 log.warn( "skip error loading properties {}", resourceName.toString() );
61 StringBuilder output = new StringBuilder();
63 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
65 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
66 output.append( '\n' );
69 return output.toString();
72 private void loadResource( Properties properties, StringBuilder resourceName, String locale )
76 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString() );
77 // if locale override with locale content
78 if ( StringUtils.isNotEmpty( locale ) )
80 loadResource( properties,
81 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString() );
86 private void loadResource( Properties properties, String resourceName )
89 InputStream is = null;
93 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
96 properties.load( is );
101 IOUtils.closeQuietly( is );