source: tapas/common/implementation/com/ether/EtherHelper.java @ 446

Last change on this file since 446 was 446, checked in by vmipsl, 12 years ago

getMessage récup du texte ds messages.properties

File size: 7.4 KB
Line 
1package com.ether;
2
3import org.apache.commons.lang.ArrayUtils;
4import org.jetbrains.annotations.NotNull;
5import org.jetbrains.annotations.Nullable;
6import org.springframework.mail.javamail.JavaMailSenderImpl;
7import org.springframework.mail.javamail.MimeMessageHelper;
8
9import javax.mail.MessagingException;
10import javax.mail.internet.MimeMessage;
11import java.io.BufferedWriter;
12import java.io.FileWriter;
13import java.io.IOException;
14import java.security.DigestException;
15import java.security.MessageDigest;
16import java.security.NoSuchAlgorithmException;
17import java.util.ArrayList;
18import java.util.List;
19import java.util.Locale;
20import java.util.Random;
21import java.util.ResourceBundle;
22
23/**
24 * @author vmipsl
25 * @date 09 sept. 2011
26 */
27public abstract class EtherHelper
28{
29    /**
30     * This method returns the list with the firts values of a list of <Pair>
31     *
32     * @param pairList
33     * @return
34     */
35    @NotNull
36    public static <T1, T2> List getFirstValues( @NotNull final List<Pair<T1, T2>> pairList )
37    {
38        final List firstValues = new ArrayList<T1>( pairList.size() );
39        for( final Pair pair : pairList )
40            firstValues.add( pair.getFirstValue() );
41
42        return firstValues;
43    }
44
45    /**
46     * This method returns the list with the second values of a list of <Pair>
47     *
48     * @param pairList
49     * @return
50     */
51    @NotNull
52    public static <T1, T2> List getSecondValues( @NotNull final List<Pair<T1, T2>> pairList )
53    {
54        final List secondValues = new ArrayList<T2>( pairList.size() );
55        for( final Pair pair : pairList )
56            secondValues.add( pair.getSecondValue() );
57
58        return secondValues;
59    }
60
61    public static double[] convertListDoubleToDoubleArray( @NotNull final List<Double> list )
62    {
63        Double[] doubleArray = new Double[list.size()];
64        doubleArray = list.toArray( doubleArray );
65        return ArrayUtils.toPrimitive( doubleArray );
66    }
67
68    @NotNull
69    public static Integer getRandomBetweenTwoInteger( @NotNull final Integer minValue, @NotNull final Integer maxValue )
70    {
71        final Random r = new Random();
72        return minValue + r.nextInt( maxValue - minValue );
73    }
74
75    @NotNull
76    public static Double getRandomBetweenTwoDouble( @NotNull final Double minValue, @NotNull final Double maxValue )
77    {
78        final Random r = new Random();
79        final Double value = minValue + r.nextDouble();
80        if( value > maxValue )
81            return getRandomBetweenTwoDouble( minValue, maxValue );
82        else
83            return value;
84    }
85
86    /**
87     * This method writes the text into the file filePath
88     *
89     * @param filePath
90     * @param text
91     */
92    public static void writeInFile( @NotNull final String filePath, @NotNull final String text )
93    {
94        try
95        {
96            final FileWriter fw = new FileWriter( filePath, true );
97            final BufferedWriter output = new BufferedWriter( fw );
98            output.write( text );
99            output.flush();
100            output.close();
101        }
102        catch( IOException ioe )
103        {
104            ioe.printStackTrace();
105        }
106
107    }
108
109    /**
110     * This method rounded a value with 'n' number after comma
111     *
112     * @param value            : the value to convert
113     * @param numberOfDecimals : the number of decimals after the comma
114     * @return the rounding value
115     */
116    public static Double floor( final Double value, final int numberOfDecimals )
117    {
118        final Double powValue = Math.pow( 10.0, numberOfDecimals );
119        return Math.floor( ( value * powValue ) + 0.5 ) / powValue;
120    }
121
122
123    /**
124     * Returns the specified String as an encrypted hash
125     *
126     * @param stringToEncrypt
127     * @return
128     */
129    @NotNull
130    public static byte[] encryptStringToHash( @NotNull final String stringToEncrypt )
131            throws DigestException, NoSuchAlgorithmException
132    {
133        final byte[] input = stringToEncrypt.getBytes();
134        final MessageDigest digest = MessageDigest.getInstance( "SHA-1" );
135        digest.update( input, 0, input.length );
136        final int hashLength = 20; // SHA-1 donne un hash de longueur 20
137        final byte[] hash = new byte[hashLength];
138        digest.digest( hash, 0, hashLength );
139        return hash;
140    }
141
142    /**
143     * Returns the specified bytes array hash as a String
144     *
145     * @param encryptedHash the hash as a bytes array
146     * @return the hash as a String
147     */
148    @NotNull
149    public static String displayEncryptedHash( @NotNull final byte[] encryptedHash )
150    {
151        final StringBuilder stringBuilder = new StringBuilder();
152        for( final byte aHash : encryptedHash )
153        {
154            final int v = aHash & 0xFF;
155            if( v < 16 )
156                stringBuilder.append( '0' );
157
158            stringBuilder.append( Integer.toString( v, 16 ) );
159        }
160        return stringBuilder.toString();
161    }
162
163    /**
164     * This method creates a encrypted password with SHA-1 hash
165     *
166     * @param password
167     * @return
168     */
169    @NotNull
170    public static String encryptPassword( @NotNull final String password )
171            throws DigestException, NoSuchAlgorithmException
172    {
173        final byte[] sHAPassword = EtherHelper.encryptStringToHash( password );
174        return EtherHelper.displayEncryptedHash( sHAPassword );
175    }
176
177
178    /**
179     * This method executes the script/command given in parameter
180     * Example of use : execProcess( "/home_local/pif.sh pof" );
181     *
182     * @param command
183     */
184    public static void execProcess( @NotNull final String command )
185            throws FormattedException
186    {
187        try
188        {
189            final Runtime runtime = Runtime.getRuntime();
190            runtime.exec( command );
191        }
192        catch( IOException e )
193        {
194            throw new FormattedException( FormattedException.FormattedCode.ERROR_IO_TO_EXECUTE_PROCESS, e );
195        }
196    }
197
198    /**
199     * This method sends a message
200     * host (see in properties) : mailhost.ipsl.jussieu.fr
201     *
202     * @param host
203     * @param to
204     * @param from
205     * @param subject
206     * @param text
207     * @throws FormattedException
208     */
209    public static void sendMessage( @NotNull final String host, @NotNull final String from, @NotNull final String to, @NotNull final String subject, @NotNull final String text )
210            throws FormattedException
211    {
212        try
213        {
214            final JavaMailSenderImpl sender = new JavaMailSenderImpl();
215            sender.setHost( host );
216
217            final MimeMessage message = sender.createMimeMessage();
218            final MimeMessageHelper helper = new MimeMessageHelper( message );
219            helper.setFrom( from );
220            helper.setTo( to );
221            helper.setSubject( subject );
222            helper.setText( text );
223            sender.send( message );
224        }
225        catch( MessagingException e )
226        {
227            throw new FormattedException( FormattedException.FormattedCode.ERROR_TO_SEND_MESSAGE, e );
228        }
229    }
230
231    /**
232     * This method return the text corresponding to the code in the files messages**.properties
233     *
234     * @param code
235     * @param locale
236     * @return
237     */
238    public static String getMessage( @NotNull final String code, @Nullable final Locale locale )
239    {
240        ResourceBundle bundle = ResourceBundle.getBundle( "messages", Locale.ENGLISH );
241        if( null != locale )
242            bundle = ResourceBundle.getBundle( "messages", locale );
243
244        return bundle.getString( code );
245    }
246
247}
Note: See TracBrowser for help on using the repository browser.