Browse Source

Change to wdcy

0.2
lishengzhao 11 years ago
parent
commit
3073b0642f
  1. 44
      src/main/java/cc/wdcy/domain/shared/BeanProvider.java
  2. 179
      src/main/java/cc/wdcy/domain/shared/paginated/DefaultPaginated.java
  3. 35
      src/main/java/cc/wdcy/domain/shared/paginated/Paginated.java
  4. 26
      src/main/java/cc/wdcy/domain/shared/paginated/PaginatedLoader.java
  5. 30
      src/main/java/cc/wdcy/domain/shared/paginated/PaginatedSort.java
  6. 22
      src/main/java/cc/wdcy/domain/shared/security/SecurityHolder.java
  7. 31
      src/main/java/cc/wdcy/domain/shared/security/SecurityUtils.java
  8. 10
      src/main/java/cc/wdcy/domain/user/User.java
  9. 33
      src/main/java/cc/wdcy/web/context/BeanContextLoaderListener.java
  10. 36
      src/main/java/cc/wdcy/web/context/SpringSecurityHolder.java
  11. 11
      src/main/java/cc/wdcy/web/controller/UserController.java
  12. 11
      src/main/java/cc/wdcy/web/controller/mobile/MobileController.java
  13. 11
      src/main/java/cc/wdcy/web/controller/unity/UnityController.java
  14. 12
      src/main/java/cc/wdcy/web/oauth/OauthUserApprovalHandler.java
  15. 6
      src/main/resources/spring/security.xml
  16. 2
      src/main/webapp/WEB-INF/web.xml
  17. 13
      src/test/java/cc/wdcy/ContextTest.java

44
src/main/java/cc/wdcy/domain/shared/BeanProvider.java

@ -1,44 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared;
import org.springframework.context.ApplicationContext;
/**
* @author Shengzhao Li
*/
public abstract class BeanProvider {
private static ApplicationContext applicationContext;
public static void initialize(ApplicationContext applicationContext) {
BeanProvider.applicationContext = applicationContext;
}
/**
* Get Bean by clazz.
*
* @param clazz Class
* @param <T> class type
* @return Bean instance
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String beanId) {
return (T) applicationContext.getBean(beanId);
}
}

179
src/main/java/cc/wdcy/domain/shared/paginated/DefaultPaginated.java

@ -1,179 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.paginated;
import cc.wdcy.domain.shared.security.SecurityUtils;
import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author Shengzhao Li
*/
public class DefaultPaginated<T> implements Paginated<T>, PaginatedList {
public static final int DEFAULT_PER_PAGE_SIZE = 20;
protected List<T> list = new ArrayList<T>();
protected int perPageSize;
protected int totalSize = 0;
protected int pageNumber = 1;
protected String sortName;
protected PaginatedSort sort;
protected String sortCriterion;
private SortOrderEnum sortOrderEnum;
private String searchId;
public DefaultPaginated() {
this(DEFAULT_PER_PAGE_SIZE);
}
public DefaultPaginated(int perPageSize) {
this.perPageSize = perPageSize;
}
@Override
public List<T> getList() {
return list;
}
public Map<String, Object> defaultQueryMap() {
Map<String, Object> map = new HashMap<>();
map.put("user", SecurityUtils.currentUser());
map.put("perPageSize", getPerPageSize());
map.put("startIndex", getStartIndex());
return map;
}
public int getTotalPages() {
if (totalSize % perPageSize == 0) {
return (totalSize / perPageSize);
} else {
return (totalSize / perPageSize) + 1;
}
}
@Override
public int getPageNumber() {
return pageNumber;
}
@Override
public int getObjectsPerPage() {
return perPageSize;
}
@Override
public int getFullListSize() {
return totalSize;
}
@Override
public String getSortCriterion() {
return sortCriterion;
}
public void setSortCriterion(String sortCriterion) {
this.sortCriterion = sortCriterion;
}
@Override
public SortOrderEnum getSortDirection() {
return sortOrderEnum;
}
@Override
public String getSearchId() {
return searchId;
}
@Override
public int getPerPageSize() {
return perPageSize;
}
@Override
public int getTotalSize() {
return totalSize;
}
@Override
public String getSortName() {
return sortName;
}
@Override
public PaginatedSort getSort() {
return sort;
}
public int getStartIndex() {
return (getPageNumber() - 1) * getPerPageSize();
}
@SuppressWarnings("unchecked")
public <K extends Paginated> K load(PaginatedLoader<T> paginatedLoader) {
if (this.totalSize == 0) {
this.totalSize = paginatedLoader.loadTotalSize();
}
this.list = paginatedLoader.loadList();
afterLoad();
return (K) this;
}
public void afterLoad() {
// Callback after load data.
}
public void setPerPageSize(int perPageSize) {
this.perPageSize = perPageSize;
}
public void setTotalSize(int totalSize) {
this.totalSize = totalSize;
}
public void setPageNumber(int pageNumber) {
this.pageNumber = pageNumber;
}
public void setSortName(String sortName) {
this.sortName = sortName;
}
public void setSort(PaginatedSort sort) {
this.sort = sort;
}
public boolean isHasNext() {
return (getStartIndex() + this.perPageSize < totalSize);
}
public boolean isHasPrevious() {
return getStartIndex() != 0;
}
public void setSortOrderEnum(SortOrderEnum sortOrderEnum) {
this.sortOrderEnum = sortOrderEnum;
}
public void setSearchId(String searchId) {
this.searchId = searchId;
}
}

35
src/main/java/cc/wdcy/domain/shared/paginated/Paginated.java

@ -1,35 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.paginated;
import java.util.List;
/**
* @author Shengzhao Li
*/
public interface Paginated<T> {
List<T> getList();
int getPageNumber();
int getPerPageSize();
int getTotalSize();
String getSortName();
PaginatedSort getSort();
int getTotalPages();
}

26
src/main/java/cc/wdcy/domain/shared/paginated/PaginatedLoader.java

@ -1,26 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.paginated;
import java.util.List;
/**
* @author Shengzhao Li
*/
public interface PaginatedLoader<T> {
List<T> loadList();
int loadTotalSize();
}

30
src/main/java/cc/wdcy/domain/shared/paginated/PaginatedSort.java

@ -1,30 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.paginated;
/**
* @author Shengzhao Li
*/
public enum PaginatedSort {
ASC("asc"),
DESC("desc");
private String label;
PaginatedSort(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
}

22
src/main/java/cc/wdcy/domain/shared/security/SecurityHolder.java

@ -1,22 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.security;
/**
* @author Shengzhao Li
*/
public interface SecurityHolder {
WdcyUserDetails userDetails();
}

31
src/main/java/cc/wdcy/domain/shared/security/SecurityUtils.java

@ -1,31 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.domain.shared.security;
import cc.wdcy.domain.user.User;
/**
* @author Shengzhao Li
*/
public class SecurityUtils {
private static SecurityHolder securityHolder;
public void setSecurityHolder(SecurityHolder securityHolder) {
SecurityUtils.securityHolder = securityHolder;
}
public static User currentUser() {
WdcyUserDetails userDetails = securityHolder.userDetails();
return (userDetails != null ? userDetails.user() : null);
}
}

10
src/main/java/cc/wdcy/domain/user/User.java

@ -12,7 +12,6 @@
package cc.wdcy.domain.user;
import cc.wdcy.domain.AbstractDomain;
import cc.wdcy.domain.shared.BeanProvider;
import java.util.Date;
@ -21,7 +20,6 @@ import java.util.Date;
*/
public class User extends AbstractDomain {
private transient UserRepository userRepository = BeanProvider.getBean(UserRepository.class);
private String username;
private String password;
@ -86,14 +84,6 @@ public class User extends AbstractDomain {
return this;
}
@Override
public void saveOrUpdate() {
if (isNewly()) {
userRepository.saveUser(this);
} else {
userRepository.updateUser(this);
}
}
public User username(String username) {
this.username = username;

33
src/main/java/cc/wdcy/web/context/BeanContextLoaderListener.java

@ -1,33 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.context;
import cc.wdcy.domain.shared.BeanProvider;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContextEvent;
/**
* @author Shengzhao Li
*/
public class BeanContextLoaderListener extends ContextLoaderListener {
@Override
public void contextInitialized(ServletContextEvent event) {
super.contextInitialized(event);
WebApplicationContext applicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(event.getServletContext());
BeanProvider.initialize(applicationContext);
}
}

36
src/main/java/cc/wdcy/web/context/SpringSecurityHolder.java

@ -1,36 +0,0 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.context;
import cc.wdcy.domain.shared.security.WdcyUserDetails;
import cc.wdcy.domain.shared.security.SecurityHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
/**
* @author Shengzhao Li
*/
public class SpringSecurityHolder implements SecurityHolder {
@Override
public WdcyUserDetails userDetails() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null) {
return null;
}
Object principal = authentication.getPrincipal();
if (principal instanceof WdcyUserDetails) {
return (WdcyUserDetails) principal;
}
return null;
}
}

11
src/main/java/cc/wdcy/web/controller/UserController.java

@ -1,14 +1,3 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.controller;
import cc.wdcy.service.UserService;

11
src/main/java/cc/wdcy/web/controller/mobile/MobileController.java

@ -1,14 +1,3 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.controller.mobile;
import org.springframework.stereotype.Controller;

11
src/main/java/cc/wdcy/web/controller/unity/UnityController.java

@ -1,14 +1,3 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.controller.unity;
import org.springframework.stereotype.Controller;

12
src/main/java/cc/wdcy/web/oauth/OauthUserApprovalHandler.java

@ -1,15 +1,3 @@
/*
* Copyright (c) 2013 Honyee Industry Group Co., Ltd
* www.honyee.biz
* All rights reserved.
*
* This software is the confidential and proprietary information of
* Honyee Industry Group Co., Ltd ("Confidential Information").
* You shall not disclose such Confidential Information and shall use
* it only in accordance with the terms of the license agreement you
* entered into with Honyee Industry Group Co., Ltd.
*/
package cc.wdcy.web.oauth;
import cc.wdcy.domain.oauth.OauthClientDetails;

6
src/main/resources/spring/security.xml

@ -171,11 +171,5 @@
Oauth server end.............
-->
<!--security holder-->
<beans:bean id="springSecurityHolder" class="cc.wdcy.web.context.SpringSecurityHolder"/>
<beans:bean id="securityHolderFactory" class="cc.wdcy.domain.shared.security.SecurityUtils">
<beans:property name="securityHolder" ref="springSecurityHolder"/>
</beans:bean>
</beans:beans>

2
src/main/webapp/WEB-INF/web.xml

@ -72,7 +72,7 @@
<!-- Spring context listener -->
<listener>
<listener-class>cc.wdcy.web.context.BeanContextLoaderListener</listener-class>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--hy mvc-->

13
src/test/java/cc/wdcy/ContextTest.java

@ -1,9 +1,5 @@
package cc.wdcy;
import cc.wdcy.domain.shared.BeanProvider;
import cc.wdcy.domain.shared.security.WdcyUserDetails;
import cc.wdcy.domain.shared.security.SecurityUtils;
import cc.wdcy.web.context.SpringSecurityHolder;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.springframework.test.context.transaction.BeforeTransaction;
@ -16,13 +12,6 @@ public abstract class ContextTest extends AbstractTransactionalTestNGSpringConte
@BeforeTransaction
public void beforeTest() {
BeanProvider.initialize(applicationContext);
SecurityUtils securityUtils = new SecurityUtils();
securityUtils.setSecurityHolder(new SpringSecurityHolder() {
@Override
public WdcyUserDetails userDetails() {
return null;
}
});
}
}
Loading…
Cancel
Save