View Javadoc
1   /*
2    * Copyright 2010 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.service.impl.action.common;
20  
21  import org.springframework.security.core.Authentication;
22  import org.springframework.security.core.context.SecurityContext;
23  import org.springframework.security.core.context.SecurityContextHolder;
24  
25  import com.hack23.cia.model.internal.application.user.impl.UserAccount;
26  import com.hack23.cia.service.api.action.common.ServiceRequest;
27  import com.hack23.cia.service.api.action.common.ServiceResponse;
28  
29  /**
30   * The Class AbstractBusinessServiceImpl.
31   *
32   * @param <T>
33   *            the generic type
34   * @param <V>
35   *            the value type
36   */
37  public abstract class AbstractBusinessServiceImpl<T extends ServiceRequest, V extends ServiceResponse>
38  		implements BusinessService<T, V> {
39  
40  	/** The clazz. */
41  	private final Class<T> clazz;
42  
43  	/**
44  	 * Instantiates a new abstract business service impl.
45  	 *
46  	 * @param clazz
47  	 *            the clazz
48  	 */
49  	public AbstractBusinessServiceImpl(final Class<T> clazz) {
50  		this.clazz = clazz;
51  	}
52  
53  	@Override
54  	public final Class<? extends ServiceRequest> getSupportedService() {
55  		return clazz;
56  	}
57  
58  	/**
59  	 * Gets the user account from security context.
60  	 *
61  	 * @return the user account from security context
62  	 */
63  	protected final UserAccount getUserAccountFromSecurityContext() {
64  		final SecurityContext context = SecurityContextHolder.getContext();
65  		if (context != null) {
66  			final Authentication authentication = context.getAuthentication();
67  			if (authentication != null) {
68  				final Object principal = authentication.getPrincipal();
69  
70  				if (principal instanceof UserAccount) {
71  					return (UserAccount) principal;
72  				}
73  			}
74  		}
75  
76  		return null;
77  	}
78  
79  
80  }