View Javadoc
1   package com;
2   
3   /*
4    * #%L
5    * Gateway
6    * %%
7    * Copyright (C) 2015 Powered by Sergey
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  
24  /**
25   * Created by aautushk on 9/26/2015.
26   */
27  
28  import org.springframework.beans.factory.annotation.Value;
29  import org.springframework.boot.SpringApplication;
30  import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
31  import org.springframework.context.MessageSource;
32  import org.springframework.context.annotation.Bean;
33  import org.springframework.context.annotation.ComponentScan;
34  import org.springframework.context.annotation.Configuration;
35  import org.springframework.context.support.ReloadableResourceBundleMessageSource;
36  import org.springframework.data.domain.AuditorAware;
37  import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
38  import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
39  import org.springframework.jdbc.datasource.DriverManagerDataSource;
40  import org.springframework.mail.javamail.JavaMailSender;
41  import org.springframework.mail.javamail.JavaMailSenderImpl;
42  import org.springframework.orm.jpa.JpaVendorAdapter;
43  import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
44  import org.springframework.orm.jpa.vendor.Database;
45  import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
46  import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
47  import org.springframework.transaction.annotation.EnableTransactionManagement;
48  import org.springframework.util.StringUtils;
49  import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
50  import org.springframework.web.bind.annotation.RestController;
51  import org.springframework.web.servlet.LocaleResolver;
52  import org.springframework.web.servlet.i18n.CookieLocaleResolver;
53  import org.thymeleaf.spring3.SpringTemplateEngine;
54  import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
55  
56  import javax.persistence.EntityManagerFactory;
57  import javax.sql.DataSource;
58  import java.util.Properties;
59  
60  @Configuration // needed to specify that the class contains global spring configurations
61  @ComponentScan
62  @EnableAutoConfiguration
63  @EnableJpaRepositories // needed to enable JPA based repositories
64  @EnableTransactionManagement
65  @RestController // needed to enable rest end points within the class
66  @EnableGlobalMethodSecurity(securedEnabled=true) // needed for method based security (@Secured("ROLE_ADMIN") annotation))
67  @EnableJpaAuditing
68  //needed to activate auditing(automatically managed fields createdBy, createdDate, lastModifiedBy, lastModifiedDate)
69  
70  public class MockGatewayApplication {
71      @Value("${db.type}")
72      private String dbType;
73  
74      @Value("${db.dialect}")
75      private String dbDialect;
76  
77      @Value("${db.driver}")
78      private String dbDriver;
79  
80      @Value("${db.url}")
81      private String dbUrl;
82  
83      @Value("${db.username}")
84      private String dbUsername;
85  
86      @Value("${db.password}")
87      private String dbPassword;
88  
89      @Value("${mailserver.host}")
90      private String mailHost;
91  
92      @Value("${mailserver.port}")
93      private int mailPort;
94  
95      @Value("${mailserver.username}")
96      private String mailUsername;
97  
98      @Value("${mailserver.password}")
99      private String mailPassword;
100 
101     private class SpringSecurityAuditorAware implements AuditorAware<String> {
102         @Override
103         public String getCurrentAuditor() {
104             return "test@gmail.com";
105         }
106     }
107 
108     @Bean
109     public DataSource dataSource(){
110         DriverManagerDataSource ds = new DriverManagerDataSource();
111         ds.setDriverClassName(dbDriver);
112         ds.setUrl(dbUrl);
113         ds.setUsername(dbUsername);
114         ds.setPassword(dbPassword);
115 
116         return ds;
117     }
118 
119     @Bean
120     public AuditorAware<String> auditorProvider() {
121         return new SpringSecurityAuditorAware();
122     }
123 
124     @Bean
125     public JpaVendorAdapter jpaVendorAdapter() {
126         HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
127         adapter.setDatabase(Database.valueOf(dbType));
128         adapter.setShowSql(true);
129         adapter.setGenerateDdl(true);
130         adapter.setDatabasePlatform(dbDialect);
131         return adapter;
132     }
133 
134     @Bean
135     public EntityManagerFactory entityManagerFactory() {
136 
137         HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
138         vendorAdapter.setDatabase(Database.valueOf(dbType));
139         vendorAdapter.setShowSql(true);
140         vendorAdapter.setGenerateDdl(false); //true value not for production !!! update db after entityManager instantiation based on entities
141         vendorAdapter.setDatabasePlatform(dbDialect);
142 
143         LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
144         factory.setJpaVendorAdapter(vendorAdapter);
145         factory.setPackagesToScan("com.entity");
146         factory.setDataSource(dataSource());
147         factory.afterPropertiesSet();
148 
149         return factory.getObject();
150     }
151 
152     @Bean
153     public javax.validation.Validator localValidatorFactoryBean() {
154         return new LocalValidatorFactoryBean();
155     }
156 
157     @Bean
158     public LocaleResolver localeResolver() {
159         CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
160         cookieLocaleResolver.setDefaultLocale(StringUtils.parseLocaleString("en"));
161         cookieLocaleResolver.setCookieName("gatewayLanguage");
162         cookieLocaleResolver.setCookieMaxAge(604800);//one month
163         return cookieLocaleResolver;
164     }
165 
166     @Bean
167     public MessageSource messageSource() {
168 
169         ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
170         messageSource.setBasenames("classpath:messages");
171         // if true, the key of the message will be displayed if the key is not
172         // found, instead of throwing a NoSuchMessageException
173         messageSource.setUseCodeAsDefaultMessage(true);
174         messageSource.setDefaultEncoding("UTF-8");
175         // # -1 : never reload, 0 always reload
176         messageSource.setCacheSeconds(0);
177         return messageSource;
178     }
179 
180     @Bean
181     public JavaMailSender mailSender(){
182         JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
183 
184 //		mailSender.setHost("localhost");
185 //		mailSender.setPort(25);
186 
187         mailSender.setHost(mailHost);
188         mailSender.setPort(mailPort);
189         mailSender.setUsername(mailUsername);
190         mailSender.setPassword(mailPassword);
191 
192         Properties properties = new Properties();
193         properties.setProperty("mail.smtp.auth", "true");
194         properties.setProperty("mail.smtp.starttls.enable", "true");
195         mailSender.setJavaMailProperties(properties);
196 
197         return mailSender;
198     }
199 
200     @Bean
201     public ClassLoaderTemplateResolver emailTemplateResolver() {
202         ClassLoaderTemplateResolver resolver =
203                 new ClassLoaderTemplateResolver();
204         resolver.setPrefix("emailTemplates/");
205         resolver.setSuffix(".html");
206         resolver.setTemplateMode("HTML5");
207         resolver.setCharacterEncoding("UTF-8");
208         return resolver;
209     }
210 
211     @Bean
212     public SpringTemplateEngine templateEngine() {
213         SpringTemplateEngine engine = new SpringTemplateEngine();
214         return engine;
215     }
216 
217     public static void main(String[] args) {
218         SpringApplication.run(GatewayApplication.class, args);
219     }
220 }