use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.
the class ActivateCGAction method execute.
@Override
public BindingResult execute(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> att) {
BindingResult response;
boolean aquired = false;
try {
checkMethod(headers);
final TCToken token = TCToken.generateToken(params);
Runnable cgAction = new Runnable() {
@Override
public void run() {
try {
tokenHandler.handleNoCardActivate(token);
// run a full GC to free some heap memory
System.gc();
System.runFinalization();
System.gc();
} catch (ThreadTerminateException ex) {
LOG.debug("Activation task terminated by an interrupt.", ex);
} catch (RuntimeException ex) {
LOG.error("Unhandled exception in activation process.", ex);
} finally {
currentTaskThread = null;
// in some cases an error does not lead to a removal of the dynamic context so remove it here
DynamicContext.remove();
}
}
};
// guard thread creation
MUTEX.acquire();
aquired = true;
Thread t = currentTaskThread;
if (t != null) {
if (token.isForceProcessing()) {
LOG.info("Stopping already running ChipGateway Protocol instance.");
t.interrupt();
// wait for other task to complete
t.join();
} else {
LOG.info("Another ChipGateway Protocol instance is already running, return status=busy.");
response = new BindingResult(BindingResultCode.REDIRECT);
response.getAuxResultData().put(AuxDataKeys.REDIRECT_LOCATION, token.finalizeBusyAddress());
return response;
}
}
// perform ChipGateway Protocol in background thread, so that we can return directly
currentTaskThread = new Thread(cgAction);
currentTaskThread.setDaemon(true);
currentTaskThread.setName("ChipGateway-Activation-" + THREAD_NUM.getAndIncrement());
currentTaskThread.start();
// create redirect
response = new BindingResult(BindingResultCode.REDIRECT);
response.getAuxResultData().put(AuxDataKeys.REDIRECT_LOCATION, token.finalizeOkAddress());
} catch (WrongMethodException ex) {
LOG.warn(ex.getMessage());
response = new BindingResult(BindingResultCode.WRONG_PARAMETER);
response.setResultMessage(ex.getMessage());
} catch (NoMethodException ex) {
LOG.error("No method given in headers, maybe wrong binging.", ex);
response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
response.setResultMessage(ex.getMessage());
} catch (InvalidRedirectUrlException | InvalidTCTokenElement ex) {
LOG.error("Failed to create TCToken.", ex);
response = ex.getBindingResult();
} catch (InterruptedException ex) {
LOG.info("ChipGateway activation interrupted.");
response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
response.setResultMessage(ex.getMessage());
} finally {
if (aquired) {
MUTEX.release();
}
}
return response;
}
use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.
the class StatusAction method execute.
@Override
public BindingResult execute(RequestBody body, Map<String, String> parameters, Headers headers, List<Attachment> attachments) {
BindingResult response;
try {
StatusRequest statusRequest = StatusRequest.convert(parameters);
response = statusHandler.handleRequest(statusRequest);
} catch (Exception e) {
response = new BindingResult(BindingResultCode.INTERNAL_ERROR);
LOG.error(e.getMessage(), e);
}
return response;
}
use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.
the class ActivateAction method processRequest.
/**
* Process the request.
*
* @param body Body of the request.
* @param params Query parameters of the request.
* @param attachments Attachments of the request.
* @param tokenUrl {@code TRUE} if {@code params} contains a TCTokenURL.
* @param showUI {@code TRUE} if {@code params} contains a ShowUI parameter.
* @param status {@code TRUE} if {@code params} contains a Status parameter.
* @return A {@link BindingResult} representing the result of the request processing.
*/
private BindingResult processRequest(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> attachments, boolean tokenUrl, boolean showUI, boolean status) {
BindingResult response = null;
if (tokenUrl) {
response = processTcToken(params);
return response;
}
if (status) {
response = processStatus(body, params, headers, attachments);
return response;
}
if (showUI) {
String requestedUI = params.get("ShowUI");
response = processShowUI(requestedUI);
return response;
}
return response;
}
use of org.openecard.addon.bind.BindingResult in project open-ecard by ecsec.
the class ActivateAction method checkRequestParameters.
/**
* Check the request for correct parameters and invoke their processing if they are ok.
*
* @param body The body of the request.
* @param params The query parameters and their values.
* @param attachments Attachments of the request.
* @return A {@link BindingResult} with an error if the parameters are not correct or one depending on the processing
* of the parameters.
*/
private BindingResult checkRequestParameters(RequestBody body, Map<String, String> params, Headers headers, List<Attachment> attachments) {
BindingResult response;
boolean emptyParms, tokenUrl, status, showUI;
emptyParms = tokenUrl = status = showUI = false;
if (params.isEmpty()) {
emptyParms = true;
}
if (params.containsKey("tcTokenURL")) {
tokenUrl = true;
}
if (params.containsKey("Status")) {
status = true;
}
if (params.containsKey("ShowUI")) {
showUI = true;
}
// only continue, when there are known parameters in the request
if (emptyParms || !(tokenUrl || status || showUI)) {
response = new BindingResult(BindingResultCode.MISSING_PARAMETER);
response.setResultMessage(lang.translationForKey(NO_ACTIVATION_PARAMETERS));
showErrorMessage(lang.translationForKey(NO_ACTIVATION_PARAMETERS));
return response;
}
// check illegal parameter combination
if ((tokenUrl && showUI) || (tokenUrl && status) || (showUI && status)) {
response = new BindingResult(BindingResultCode.WRONG_PARAMETER);
response.setResultMessage(lang.translationForKey(NO_PARAMS));
showErrorMessage(lang.translationForKey(NO_PARAMS));
return response;
}
return processRequest(body, params, headers, attachments, tokenUrl, showUI, status);
}
Aggregations