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.ByteArrayInputStream;
34 import java.io.IOException;
35 import java.io.InputStream;
37 import java.util.Properties;
40 * @author Olivier Lamy
42 @Service( "commonServices#rest" )
43 public class DefaultCommonServices
44 implements CommonServices
47 private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
49 private Logger log = LoggerFactory.getLogger( getClass() );
52 private UtilServices utilServices;
54 public String getI18nResources( String locale )
55 throws ArchivaRestServiceException
57 Properties properties = new Properties();
59 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
63 loadResource( properties, resourceName, locale );
66 catch ( IOException e )
68 log.warn( "skip error loading properties {}", resourceName.toString() );
71 return fromProperties( properties );
74 private void loadResource( Properties properties, StringBuilder resourceName, String locale )
78 loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
79 // if locale override with locale content
80 if ( StringUtils.isNotEmpty( locale ) )
82 loadResource( properties,
83 new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
89 private String fromProperties( final Properties properties )
91 StringBuilder output = new StringBuilder();
93 for ( Map.Entry<Object, Object> entry : properties.entrySet() )
95 output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
96 output.append( '\n' );
99 return output.toString();
102 private void loadResource( final Properties finalProperties, String resourceName, String locale )
105 InputStream is = null;
106 Properties properties = new Properties();
109 is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName.toString() );
112 properties.load( is );
113 finalProperties.putAll( properties );
117 if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
119 log.info( "cannot load resource {}", resourceName );
125 IOUtils.closeQuietly( is );
129 public String getAllI18nResources( String locale )
130 throws ArchivaRestServiceException
135 Properties all = utilServices.getI18nProperties( locale );
136 StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
137 loadResource( all, resourceName, locale );
139 return fromProperties( all );
141 catch ( IOException e )
143 throw new ArchivaRestServiceException( e.getMessage(),
144 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
146 catch ( RedbackServiceException e )
148 throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode() );
152 private void loadFromString( String propsStr, Properties properties )
153 throws ArchivaRestServiceException
155 InputStream inputStream = null;
158 inputStream = new ByteArrayInputStream( propsStr.getBytes() );
159 properties.load( inputStream );
161 catch ( IOException e )
163 throw new ArchivaRestServiceException( e.getMessage(),
164 Response.Status.INTERNAL_SERVER_ERROR.getStatusCode() );
168 IOUtils.closeQuietly( inputStream );
172 public Boolean devMode()
174 return Boolean.getBoolean( "archiva.devMode" );