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( 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( Properties properties, String resourceName )
102 InputStream is = null;
106 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
109 properties.load( is );
114 IOUtils.closeQuietly( is );
118 public String getAllI18nResources( String locale )
119 throws ArchivaRestServiceException
123 String redbackProps = utilServices.getI18nResources( locale );
124 String archivaProps = getI18nResources( locale );
125 Properties properties = new Properties();
126 loadFromString( redbackProps, properties );
127 loadFromString( archivaProps, properties );
128 return fromProperties( properties );
130 catch ( RedbackServiceException e )
132 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
136 private void loadFromString( String propsStr, Properties properties )
137 throws ArchivaRestServiceException
140 StringReader stringReader = null;
143 stringReader = new StringReader( propsStr );
144 properties.load( stringReader );
146 catch ( IOException e )
148 throw new ArchivaRestServiceException( e.getMessage(),
149 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
153 IOUtils.closeQuietly( stringReader );