Fixed for Liferay 6.2

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/admin/accounting-manager@142553 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2017-02-14 18:37:57 +00:00
parent ca2e1ac6dc
commit 297df7e401
7 changed files with 66 additions and 70 deletions

View File

@ -3,10 +3,10 @@ package org.gcube.portlets.admin.accountingmanager.server;
import java.util.HashMap;
import java.util.concurrent.Callable;
import javax.servlet.AsyncContext;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCaller;
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerInterface;
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCallerTester;
@ -33,16 +33,26 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
super();
this.taskRequest = taskRequest;
this.accountingCache = accountingCache;
logger.debug("AccountingClientCallable: "+taskRequest);
}
@Override
public TaskStatus call() throws Exception {
try {
AsyncContext asyncContext = taskRequest.getAsyncContext();
ServletRequest servletRequest = asyncContext.getRequest();
HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
HttpSession httpSession = taskRequest.getHttpSession();
if(httpSession==null){
logger.error("Error retrieving HttpSession in AccountingClientCallable: is null");
return TaskStatus.ERROR;
}
logger.info("Set SecurityToken: "+taskRequest.getServiceCredentials().getToken());
SecurityTokenProvider.instance.set(taskRequest.getServiceCredentials().getToken());
logger.info("Set ScopeProvider: "+taskRequest.getServiceCredentials().getScope());
ScopeProvider.instance.set(taskRequest.getServiceCredentials().getScope());
AccountingCallerInterface accountingCaller;
if (Constants.DEBUG_MODE) {
accountingCaller = new AccountingCallerTester();
@ -52,7 +62,7 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
SeriesResponse seriesResponse = null;
try {
try {
seriesResponse = accountingCaller.getSeries(
taskRequest.getAccountingType(),
taskRequest.getSeriesRequest());
@ -64,12 +74,12 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
e.getLocalizedMessage());
HashMap<String, TaskWrapper> taskWrapperMap = SessionUtil
.getTaskWrapperMap(httpRequest,
.getTaskWrapperMap(httpSession,
taskRequest.getServiceCredentials());
if (taskWrapperMap == null) {
taskWrapperMap = new HashMap<>();
SessionUtil
.setTaskWrapperMap(httpRequest,
.setTaskWrapperMap(httpSession,
taskRequest.getServiceCredentials(),
taskWrapperMap);
@ -77,7 +87,6 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
taskWrapperMap.put(taskWrapper.getOperationId(), taskWrapper);
asyncContext.complete();
return TaskStatus.ERROR;
}
@ -93,7 +102,7 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
AccountingStateData accountingStateData = new AccountingStateData(
taskRequest.getAccountingType(),
taskRequest.getSeriesRequest(), seriesResponse);
SessionUtil.setAccountingStateData(httpRequest,
SessionUtil.setAccountingStateData(httpSession,
taskRequest.getServiceCredentials(),
taskRequest.getAccountingType(), accountingStateData);
@ -102,19 +111,17 @@ public class AccountingClientCallable implements Callable<TaskStatus> {
seriesResponse);
HashMap<String, TaskWrapper> taskWrapperMap = SessionUtil
.getTaskWrapperMap(httpRequest,
.getTaskWrapperMap(httpSession,
taskRequest.getServiceCredentials());
if (taskWrapperMap == null) {
taskWrapperMap = new HashMap<>();
SessionUtil.setTaskWrapperMap(httpRequest,
SessionUtil.setTaskWrapperMap(httpSession,
taskRequest.getServiceCredentials(), taskWrapperMap);
}
taskWrapperMap.put(taskWrapper.getOperationId(), taskWrapper);
asyncContext.complete();
return TaskStatus.COMPLETED;
} catch (Throwable e) {

View File

@ -28,8 +28,8 @@ public class AccountingClientListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
logger.info("Initializing AccountingCache");
accountingCache=null;
logger.info("Initializing AccountingCache");
accountingCache = null;
try {
accountingCache = new AccountingCache();
} catch (ServiceException e) {
@ -37,12 +37,13 @@ public class AccountingClientListener implements ServletContextListener {
"Error initializing AccountingCache: "
+ e.getLocalizedMessage(), e);
}
sce.getServletContext().setAttribute(SessionConstants.ACCOUNTING_CACHE, accountingCache);
sce.getServletContext().setAttribute(SessionConstants.ACCOUNTING_CACHE,
accountingCache);
logger.info("AccountingCache value saved in context.");
accountingClientDaemon = new AccountingClientDaemon(sce,accountingCache);
accountingClientDaemon = new AccountingClientDaemon(sce,
accountingCache);
thread = new Thread(accountingClientDaemon);
logger.info("Starting AccountingClientDaemon: " + thread);
thread.start();
@ -69,13 +70,12 @@ public class AccountingClientListener implements ServletContextListener {
"Error in finalize AccountingCache: "
+ e.getLocalizedMessage(), e);
}
ServletContext sc = sce.getServletContext();
sc.removeAttribute(SessionConstants.ACCOUNTING_CACHE);
logger.info("AccountingCache value deleted from context.");
}
}

View File

@ -5,11 +5,9 @@ import java.util.HashMap;
import java.util.Queue;
import java.util.UUID;
import javax.servlet.AsyncContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.gcube.portlets.admin.accountingmanager.client.rpc.AccountingManagerService;
import org.gcube.portlets.admin.accountingmanager.server.amservice.AccountingCaller;
@ -156,7 +154,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
AccountingStateData accountingStateData = new AccountingStateData(
accountingType, seriesRequest, seriesResponse);
SessionUtil.setAccountingStateData(httpRequest, serviceCredentials,
SessionUtil.setAccountingStateData(httpRequest.getSession(), serviceCredentials,
accountingType, accountingStateData);
return seriesResponse;
@ -182,14 +180,16 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
ServiceCredentials serviceCredentials = SessionUtil
.getServiceCredentials(httpRequest);
HttpServletResponse httpResponse = this.getThreadLocalResponse();
//HttpServletResponse httpResponse = this.getThreadLocalResponse();
AsyncContext asyncContext = httpRequest.startAsync(httpRequest,
httpResponse);
/*AsyncContext asyncContext = httpRequest.startAsync(httpRequest,
httpResponse);*/
String operationId = UUID.randomUUID().toString();
logger.info("Accounting Task Operation Id: " + operationId);
logger.info("Session Id: "+httpRequest.getSession());
TaskRequest taskRequest = new TaskRequest(operationId,
asyncContext, serviceCredentials, accountingType,
httpRequest.getSession(), serviceCredentials, accountingType,
seriesRequest);
ServletContext appScope = httpRequest.getServletContext();
@ -222,7 +222,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
.getServiceCredentials(httpRequest);
HashMap<String, TaskWrapper> taskWrapperMap = SessionUtil
.getTaskWrapperMap(httpRequest, serviceCredentials);
.getTaskWrapperMap(httpRequest.getSession(), serviceCredentials);
if (taskWrapperMap == null) {
return null;
} else {
@ -359,7 +359,7 @@ public class AccountingManagerServiceImpl extends RemoteServiceServlet
logger.debug("SaveDataOnWorkspace(): " + accountingType);
AccountingStateData accountingStateData = SessionUtil
.getAccountingStateData(httpRequest, serviceCredentials,
.getAccountingStateData(httpRequest.getSession(), serviceCredentials,
accountingType);
if (accountingStateData == null) {
logger.error("No series present in session for thi accounting type: "

View File

@ -112,7 +112,7 @@ public class ExportServlet extends HttpServlet {
+ ", accountingType=" + accountingType + "]");
AccountingStateData accountingStateData = SessionUtil
.getAccountingStateData(request,
.getAccountingStateData(session,
serviceCredentials, AccountingType.valueOf(accountingType));
if (accountingStateData == null) {
logger.error("No series present in session for this accounting type: "

View File

@ -197,11 +197,10 @@ public class SessionUtil {
return sCredentials;
}
public static void setAccountingStateData(HttpServletRequest httpRequest,
public static void setAccountingStateData(HttpSession httpSession,
ServiceCredentials serviceCredentials,
AccountingType accountingType,
AccountingStateData accountingStateData) {
HttpSession httpSession = httpRequest.getSession();
@SuppressWarnings("unchecked")
HashMap<String, AccountingState> accountingStateMap = (HashMap<String, AccountingState>) httpSession
.getAttribute(Constants.SESSION_ACCOUNTING_STATE_MAP);
@ -234,9 +233,9 @@ public class SessionUtil {
}
public static AccountingStateData getAccountingStateData(
HttpServletRequest httpRequest, ServiceCredentials serviceCredentials,
HttpSession httpSession, ServiceCredentials serviceCredentials,
AccountingType accountingType) {
HttpSession httpSession = httpRequest.getSession();
@SuppressWarnings("unchecked")
HashMap<String, AccountingState> accountingStateMap = (HashMap<String, AccountingState>) httpSession
.getAttribute(Constants.SESSION_ACCOUNTING_STATE_MAP);
@ -265,14 +264,12 @@ public class SessionUtil {
* @return
*/
public static HashMap<String, TaskWrapper> getTaskWrapperMap(
HttpServletRequest httpRequest,
HttpSession httpSession,
ServiceCredentials serviceCredentials) {
SessionOp<HashMap<String, TaskWrapper>> sessionOp = new SessionOp<>();
HashMap<String, TaskWrapper> taskWrapperMap = sessionOp.get(
httpRequest, serviceCredentials,
SessionConstants.TASK_WRAPPER_MAP);
@SuppressWarnings("unchecked")
HashMap<String, TaskWrapper> taskWrapperMap = (HashMap<String, TaskWrapper>) httpSession
.getAttribute(SessionConstants.TASK_WRAPPER_MAP);
return taskWrapperMap;
}
/**
@ -281,13 +278,11 @@ public class SessionUtil {
* @param serviceCredentials
* @param taskWrapperMap
*/
public static void setTaskWrapperMap(HttpServletRequest httpRequest,
public static void setTaskWrapperMap(HttpSession httpSession,
ServiceCredentials serviceCredentials,
HashMap<String, TaskWrapper> taskWrapperMap) {
SessionOp<HashMap<String, TaskWrapper>> sessionOp = new SessionOp<>();
sessionOp.set(httpRequest, serviceCredentials,
SessionConstants.TASK_WRAPPER_MAP, taskWrapperMap);
httpSession.setAttribute(SessionConstants.TASK_WRAPPER_MAP, taskWrapperMap);
return;
}
public static Context getContext(ServiceCredentials serviceCredentials)

View File

@ -2,7 +2,7 @@ package org.gcube.portlets.admin.accountingmanager.server.util;
import java.io.Serializable;
import javax.servlet.AsyncContext;
import javax.servlet.http.HttpSession;
import org.gcube.portlets.admin.accountingmanager.shared.data.AccountingType;
import org.gcube.portlets.admin.accountingmanager.shared.data.query.SeriesRequest;
@ -17,28 +17,21 @@ public class TaskRequest implements Serializable {
private static final long serialVersionUID = -4010108343968344171L;
private String operationId;
private AsyncContext asyncContext;
private HttpSession httpSession;
private ServiceCredentials serviceCredentials;
private AccountingType accountingType;
private SeriesRequest seriesRequest;
/**
*
* @param operationId
* @param asyncContext
* @param serviceCredentials
* @param accountingType
* @param seriesRequest
*/
public TaskRequest(String operationId, AsyncContext asyncContext,
public TaskRequest(String operationId, HttpSession httpSession,
ServiceCredentials serviceCredentials,
AccountingType accountingType, SeriesRequest seriesRequest) {
super();
this.operationId = operationId;
this.asyncContext = asyncContext;
this.httpSession = httpSession;
this.serviceCredentials = serviceCredentials;
this.accountingType = accountingType;
this.seriesRequest = seriesRequest;
}
public String getOperationId() {
@ -49,12 +42,12 @@ public class TaskRequest implements Serializable {
this.operationId = operationId;
}
public AsyncContext getAsyncContext() {
return asyncContext;
public HttpSession getHttpSession() {
return httpSession;
}
public void setAsyncContext(AsyncContext asyncContext) {
this.asyncContext = asyncContext;
public void setHttpSession(HttpSession httpSession) {
this.httpSession = httpSession;
}
public ServiceCredentials getServiceCredentials() {
@ -83,8 +76,8 @@ public class TaskRequest implements Serializable {
@Override
public String toString() {
return "TaskRequest [operationId=" + operationId + ", asyncContext="
+ asyncContext + ", serviceCredentials=" + serviceCredentials
return "TaskRequest [operationId=" + operationId + ", httpSession="
+ httpSession + ", serviceCredentials=" + serviceCredentials
+ ", accountingType=" + accountingType + ", seriesRequest="
+ seriesRequest + "]";
}

View File

@ -26,6 +26,7 @@
<servlet>
<servlet-name>ExportServlet</servlet-name>
<servlet-class>org.gcube.portlets.admin.accountingmanager.server.ExportServlet</servlet-class>
<async-supported>true</async-supported>
</servlet>
<!-- Servlets Mapping -->