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.codehaus.redback.rest.api.services.RedbackServiceException;
26 import org.codehaus.redback.rest.api.services.UtilServices;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.stereotype.Service;
31 import javax.inject.Inject;
32 import javax.ws.rs.core.Response;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.io.StringReader;
37 import java.util.Properties;
40 * @author Olivier Lamy
42 @Service( "commonServices#rest" )
43 public class DefaultCommonServices
44 implements CommonServices
47 private Logger log = LoggerFactory.getLogger( getClass() );
50 private UtilServices utilServices;
52 public String getI18nResources( String locale )
53 throws ArchivaRestServiceException
55 Properties properties = new Properties();
57 StringBuilder resourceName = new StringBuilder( "org/apache/archiva/i18n/default" );
61 loadResource( properties, resourceName, locale );
64 catch ( IOException e )
66 log.warn( "skip error loading properties {}", resourceName.toString() );
69 return fromProperties( properties );
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 String fromProperties( final Properties properties )
88 StringBuilder output = new StringBuilder();
90 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
92 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
93 output.append( '\n' );
96 return output.toString();
99 private void loadResource( final Properties finalProperties, String resourceName )
102 InputStream is = null;
103 Properties properties = new Properties();
106 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
109 properties.load( is );
110 finalProperties.putAll( properties );
114 log.info( "cannot load resource {}", resourceName );
119 IOUtils.closeQuietly( is );
123 public String getAllI18nResources( String locale )
124 throws ArchivaRestServiceException
128 String redbackProps = utilServices.getI18nResources( locale );
129 String archivaProps = getI18nResources( locale );
130 Properties properties = new Properties();
131 loadFromString( redbackProps, properties );
132 loadFromString( archivaProps, properties );
133 return fromProperties( properties );
135 catch ( RedbackServiceException e )
137 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
141 private void loadFromString( String propsStr, Properties properties )
142 throws ArchivaRestServiceException
145 StringReader stringReader = null;
148 stringReader = new StringReader( propsStr );
149 properties.load( stringReader );
151 catch ( IOException e )
153 throw new ArchivaRestServiceException( e.getMessage(),
154 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
158 IOUtils.closeQuietly( stringReader );