This project has retired. For details please refer to its Attic page.
Source code
001package org.apache.archiva.rest.services;
002/*
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *   http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing,
014 * software distributed under the License is distributed on an
015 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
016 * KIND, either express or implied.  See the License for the
017 * specific language governing permissions and limitations
018 * under the License.
019 */
020
021import org.apache.archiva.redback.components.scheduler.CronExpressionValidator;
022import org.apache.archiva.redback.rest.api.services.RedbackServiceException;
023import org.apache.archiva.redback.rest.api.services.UtilServices;
024import org.apache.archiva.rest.api.services.ArchivaRestServiceException;
025import org.apache.archiva.rest.api.services.CommonServices;
026import org.apache.commons.lang.StringUtils;
027import org.slf4j.Logger;
028import org.slf4j.LoggerFactory;
029import org.springframework.stereotype.Service;
030
031import javax.annotation.PostConstruct;
032import javax.inject.Inject;
033import javax.ws.rs.core.Response;
034import java.io.ByteArrayInputStream;
035import java.io.IOException;
036import java.io.InputStream;
037import java.util.Map;
038import java.util.Properties;
039import java.util.concurrent.ConcurrentHashMap;
040
041/**
042 * @author Olivier Lamy
043 */
044@Service("commonServices#rest")
045public class DefaultCommonServices
046    implements CommonServices
047{
048
049    private static final String RESOURCE_NAME = "org/apache/archiva/i18n/default";
050
051    private Logger log = LoggerFactory.getLogger( getClass() );
052
053    @Inject
054    private UtilServices utilServices;
055
056    private Map<String, String> cachei18n = new ConcurrentHashMap<String, String>();
057
058    @Inject
059    protected CronExpressionValidator cronExpressionValidator;
060
061    @PostConstruct
062    public void init()
063        throws ArchivaRestServiceException
064    {
065
066        // preload i18n en and fr
067        getAllI18nResources( "en" );
068        getAllI18nResources( "fr" );
069    }
070
071    @Override
072    public String getI18nResources( String locale )
073        throws ArchivaRestServiceException
074    {
075        Properties properties = new Properties();
076
077        StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
078        try
079        {
080
081            loadResource( properties, resourceName, locale );
082
083        }
084        catch ( IOException e )
085        {
086            log.warn( "skip error loading properties {}", resourceName.toString() );
087        }
088
089        return fromProperties( properties );
090    }
091
092    private void loadResource( Properties properties, StringBuilder resourceName, String locale )
093        throws IOException
094    {
095        // load default
096        loadResource( properties, new StringBuilder( resourceName ).append( ".properties" ).toString(), locale );
097        // if locale override with locale content
098        if ( StringUtils.isNotEmpty( locale ) )
099        {
100            loadResource( properties,
101                          new StringBuilder( resourceName ).append( "_" + locale ).append( ".properties" ).toString(),
102                          locale );
103        }
104
105    }
106
107    private String fromProperties( final Properties properties )
108    {
109        StringBuilder output = new StringBuilder();
110
111        for ( Map.Entry<Object, Object> entry : properties.entrySet() )
112        {
113            output.append( (String) entry.getKey() ).append( '=' ).append( (String) entry.getValue() );
114            output.append( '\n' );
115        }
116
117        return output.toString();
118    }
119
120    private void loadResource( final Properties finalProperties, String resourceName, String locale )
121        throws IOException
122    {
123        Properties properties = new Properties();
124        try (InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( resourceName ))
125        {
126            if ( is != null )
127            {
128                properties.load( is );
129                finalProperties.putAll( properties );
130            }
131            else
132            {
133                if ( !StringUtils.equalsIgnoreCase( locale, "en" ) )
134                {
135                    log.info( "cannot load resource {}", resourceName );
136                }
137            }
138        }
139    }
140
141    @Override
142    public String getAllI18nResources( String locale )
143        throws ArchivaRestServiceException
144    {
145
146        String cachedi18n = cachei18n.get( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ) );
147        if ( cachedi18n != null )
148        {
149            return cachedi18n;
150        }
151
152        try
153        {
154
155            Properties all = utilServices.getI18nProperties( locale );
156            StringBuilder resourceName = new StringBuilder( RESOURCE_NAME );
157            loadResource( all, resourceName, locale );
158
159            String i18n = fromProperties( all );
160            cachei18n.put( StringUtils.isEmpty( locale ) ? "en" : StringUtils.lowerCase( locale ), i18n );
161            return i18n;
162        }
163        catch ( IOException e )
164        {
165            throw new ArchivaRestServiceException( e.getMessage(),
166                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
167        }
168        catch ( RedbackServiceException e )
169        {
170            throw new ArchivaRestServiceException( e.getMessage(), e.getHttpErrorCode(), e );
171        }
172    }
173
174    private void loadFromString( String propsStr, Properties properties )
175        throws ArchivaRestServiceException
176    {
177        try (InputStream inputStream = new ByteArrayInputStream( propsStr.getBytes() ))
178        {
179            properties.load( inputStream );
180        }
181        catch ( IOException e )
182        {
183            throw new ArchivaRestServiceException( e.getMessage(),
184                                                   Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(), e );
185        }
186    }
187
188
189    @Override
190    public Boolean validateCronExpression( String cronExpression )
191        throws ArchivaRestServiceException
192    {
193        return cronExpressionValidator.validate( cronExpression );
194    }
195}