summaryrefslogtreecommitdiff
path: root/STALService/src/main/java/at/gv
diff options
context:
space:
mode:
authorclemenso <clemenso@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2009-01-22 13:21:50 +0000
committerclemenso <clemenso@8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4>2009-01-22 13:21:50 +0000
commit212bbffc24ccc1cd909cabdc9650491dd221cd60 (patch)
tree8dcd8b042ed6fab53cfecd5df251acf1da8b5d04 /STALService/src/main/java/at/gv
parent041e02b585ff5f90e806dfb96c3065620aa655b1 (diff)
downloadmocca-212bbffc24ccc1cd909cabdc9650491dd221cd60.tar.gz
mocca-212bbffc24ccc1cd909cabdc9650491dd221cd60.tar.bz2
mocca-212bbffc24ccc1cd909cabdc9650491dd221cd60.zip
STALTranslator
git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@283 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4
Diffstat (limited to 'STALService/src/main/java/at/gv')
-rw-r--r--STALService/src/main/java/at/gv/egiz/stal/service/translator/STALTranslator.java274
-rw-r--r--STALService/src/main/java/at/gv/egiz/stal/service/translator/TranslationException.java42
-rw-r--r--STALService/src/main/java/at/gv/egiz/stal/util/STALTranslator.java114
3 files changed, 316 insertions, 114 deletions
diff --git a/STALService/src/main/java/at/gv/egiz/stal/service/translator/STALTranslator.java b/STALService/src/main/java/at/gv/egiz/stal/service/translator/STALTranslator.java
new file mode 100644
index 00000000..c4a3d62e
--- /dev/null
+++ b/STALService/src/main/java/at/gv/egiz/stal/service/translator/STALTranslator.java
@@ -0,0 +1,274 @@
+/*
+ * Copyright 2008 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.egiz.stal.service.translator;
+
+import at.gv.egiz.stal.ErrorResponse;
+import at.gv.egiz.stal.InfoboxReadRequest;
+import at.gv.egiz.stal.InfoboxReadResponse;
+import at.gv.egiz.stal.QuitRequest;
+import at.gv.egiz.stal.STALRequest;
+import at.gv.egiz.stal.STALResponse;
+import at.gv.egiz.stal.SignRequest;
+import at.gv.egiz.stal.SignResponse;
+import at.gv.egiz.stal.service.types.ErrorResponseType;
+import at.gv.egiz.stal.service.types.InfoboxReadRequestType;
+import at.gv.egiz.stal.service.types.InfoboxReadResponseType;
+import at.gv.egiz.stal.service.types.ObjectFactory;
+import at.gv.egiz.stal.service.types.QuitRequestType;
+import at.gv.egiz.stal.service.types.RequestType;
+import at.gv.egiz.stal.service.types.ResponseType;
+import at.gv.egiz.stal.service.types.SignRequestType;
+import at.gv.egiz.stal.service.types.SignResponseType;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.xml.bind.JAXBElement;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ *
+ * @author Clemens Orthacker <clemens.orthacker@iaik.tugraz.at>
+ */
+public class STALTranslator {
+
+ private static final Log log = LogFactory.getLog(STALTranslator.class);
+ protected Map<Class, TranslationHandler> handlerMap = new HashMap<Class, TranslationHandler>();
+
+ public STALTranslator() {
+ registerTranslationHandler(new DefaultTranslationHandler());
+ }
+
+ /**
+ * overwrites previously registered handlers for the same type
+ * @param handler
+ */
+ public void registerTranslationHandler(TranslationHandler handler) {
+ for (Class t : handler.getSupportedTypes()) {
+ if (log.isDebugEnabled()) {
+ log.debug("register " + t + " with translation handler " + handler.getClass());
+ }
+ handlerMap.put(t, handler);
+ }
+ }
+
+ /**
+ * Translate a STAL request to a STAL Webservice request.
+ * @param request
+ * @return
+ * @throws at.gv.egiz.stal.service.translator.TranslationException
+ */
+ public JAXBElement<? extends RequestType> translate(STALRequest request) throws TranslationException {
+ if (handlerMap.containsKey(request.getClass())) {
+ TranslationHandler handler = handlerMap.get(request.getClass());
+ JAXBElement<? extends RequestType> r = handler.translate(request);
+ if (r != null) {
+ return r;
+ }
+ }
+ log.error("unknown STAL request type " + request.getClass());
+ throw new TranslationException(request.getClass());
+ }
+
+ /**
+ * Translate a STAL Webservice request to a STAL request.
+ * @param request
+ * @return
+ * @throws at.gv.egiz.stal.service.translator.TranslationException
+ */
+ public STALRequest translate(JAXBElement<? extends RequestType> request) throws TranslationException {
+ RequestType req = request.getValue();
+ if (req == null) {
+ throw new RuntimeException("RequestType must not be null");
+ }
+ if (handlerMap.containsKey(req.getClass())) {
+ TranslationHandler handler = handlerMap.get(req.getClass());
+ STALRequest stalRequest = handler.translate(req);
+ if (stalRequest != null) {
+ return stalRequest;
+ }
+ }
+ log.error("unknown request type " + req.getClass());
+ throw new TranslationException(req.getClass());
+ }
+
+ /**
+ * Translate a STAL response to a STAL Webservice response.
+ * @param request
+ * @return
+ * @throws at.gv.egiz.stal.service.translator.TranslationException
+ */
+ public JAXBElement<? extends ResponseType> translate(STALResponse response) throws TranslationException {
+ if (handlerMap.containsKey(response.getClass())) {
+ TranslationHandler handler = handlerMap.get(response.getClass());
+ JAXBElement<? extends ResponseType> r = handler.translate(response);
+ if (r != null) {
+ return r;
+ }
+ }
+ log.error("unknown STAL response type " + response.getClass());
+ throw new TranslationException(response.getClass());
+ }
+
+ /**
+ * Translate a STAL Webservice response to a STAL response.
+ * @param request
+ * @return
+ * @throws at.gv.egiz.stal.service.translator.TranslationException
+ */
+ public STALResponse translate(JAXBElement<? extends ResponseType> response) throws TranslationException {
+ ResponseType resp = response.getValue();
+ if (resp == null) {
+ throw new RuntimeException("ResponseType must not be null");
+ }
+ if (handlerMap.containsKey(resp.getClass())) {
+ TranslationHandler handler = handlerMap.get(resp.getClass());
+ STALResponse stalResponse = handler.translate(resp);
+ if (stalResponse != null) {
+ return stalResponse;
+ }
+ }
+ log.error("unknown response type " + resp.getClass());
+ throw new TranslationException(resp.getClass());
+ }
+
+ /**
+ * public (static) interface implemented by STAL extensions
+ */
+ public static interface TranslationHandler {
+
+ List<Class> getSupportedTypes();
+
+ JAXBElement<? extends RequestType> translate(STALRequest request) throws TranslationException;
+
+ STALRequest translate(RequestType request) throws TranslationException;
+
+ JAXBElement<? extends ResponseType> translate(STALResponse response) throws TranslationException;
+
+ STALResponse translate(ResponseType response) throws TranslationException;
+ }
+
+
+ /**
+ * Default Handler
+ */
+ protected static class DefaultTranslationHandler implements TranslationHandler {
+
+ private static final Log log = LogFactory.getLog(DefaultTranslationHandler.class);
+ private ObjectFactory of;
+
+ public DefaultTranslationHandler() {
+ of = new ObjectFactory();
+ }
+
+ @Override
+ public List<Class> getSupportedTypes() {
+// log.warn(" *** Fake STALTranslator ***");
+ return Arrays.asList(new Class[]{InfoboxReadRequest.class,
+ SignRequest.class,
+ QuitRequest.class,
+ InfoboxReadRequestType.class,
+ SignRequestType.class,
+ QuitRequestType.class,
+ InfoboxReadResponse.class,
+ SignResponse.class,
+ ErrorResponse.class,
+ InfoboxReadResponseType.class,
+ SignResponseType.class,
+ ErrorResponseType.class
+ });
+ }
+
+ @Override
+ public JAXBElement<? extends RequestType> translate(STALRequest request) throws TranslationException {
+ log.trace("translate " + request.getClass());
+ if (request instanceof SignRequest) {
+ SignRequestType req = of.createSignRequestType();
+ req.setKeyIdentifier(((SignRequest) request).getKeyIdentifier());
+ req.setSignedInfo(((SignRequest) request).getSignedInfo());
+ //TODO add hashdatainput (refactor signRequestType)
+ return of.createGetNextRequestResponseTypeSignRequest(req);
+ } else if (request instanceof InfoboxReadRequest) {
+ InfoboxReadRequestType req = of.createInfoboxReadRequestType();
+ req.setInfoboxIdentifier(((InfoboxReadRequest) request).getInfoboxIdentifier());
+ req.setDomainIdentifier(((InfoboxReadRequest) request).getDomainIdentifier());
+ return of.createGetNextRequestResponseTypeInfoboxReadRequest(req);
+ } else if (request instanceof QuitRequest) {
+ return of.createGetNextRequestResponseTypeQuitRequest(of.createQuitRequestType());
+ }
+ throw new TranslationException(request.getClass());
+ }
+
+ @Override
+ public STALRequest translate(RequestType request) throws TranslationException {
+ if (request instanceof InfoboxReadRequestType) {
+ InfoboxReadRequest stalReq = new InfoboxReadRequest();
+ stalReq.setDomainIdentifier(((InfoboxReadRequestType) request).getDomainIdentifier());
+ stalReq.setInfoboxIdentifier(((InfoboxReadRequestType) request).getInfoboxIdentifier());
+ return stalReq;
+ } else if (request instanceof SignRequestType) {
+ SignRequest stalReq = new SignRequest();
+ stalReq.setKeyIdentifier(((SignRequestType) request).getKeyIdentifier());
+ stalReq.setSignedInfo(((SignRequestType) request).getSignedInfo());
+ return stalReq;
+ } else if (request instanceof QuitRequestType) {
+ return new QuitRequest();
+ }
+ throw new TranslationException(request.getClass());
+ }
+
+ @Override
+ public JAXBElement<? extends ResponseType> translate(STALResponse response) throws TranslationException {
+ if (response instanceof InfoboxReadResponse) {
+ InfoboxReadResponseType resp = of.createInfoboxReadResponseType();
+ resp.setInfoboxValue(((InfoboxReadResponse) response).getInfoboxValue());
+ return of.createGetNextRequestTypeInfoboxReadResponse(resp);
+ } else if (response instanceof SignResponse) {
+ SignResponseType resp = of.createSignResponseType();
+ resp.setSignatureValue(((SignResponse) response).getSignatureValue());
+ return of.createGetNextRequestTypeSignResponse(resp);
+ } else if (response instanceof ErrorResponse) {
+ ErrorResponseType resp = of.createErrorResponseType();
+ resp.setErrorCode(((ErrorResponse) response).getErrorCode());
+ resp.setErrorMessage(((ErrorResponse) response).getErrorMessage());
+ return of.createGetNextRequestTypeErrorResponse(resp);
+ }
+ throw new TranslationException(response.getClass());
+ }
+
+ @Override
+ public STALResponse translate(ResponseType response) throws TranslationException {
+ if (response instanceof InfoboxReadResponseType) {
+ InfoboxReadResponse stalResp = new InfoboxReadResponse();
+ stalResp.setInfoboxValue(((InfoboxReadResponseType) response).getInfoboxValue());
+ return stalResp;
+ } else if (response instanceof SignResponseType) {
+ SignResponse stalResp = new SignResponse();
+ stalResp.setSignatureValue(((SignResponseType) response).getSignatureValue());
+ return stalResp;
+ } else if (response instanceof ErrorResponseType) {
+ ErrorResponse stalResp = new ErrorResponse();
+ stalResp.setErrorCode(((ErrorResponseType) response).getErrorCode());
+ stalResp.setErrorMessage(((ErrorResponseType) response).getErrorMessage());
+ return stalResp;
+ }
+ throw new TranslationException(response.getClass());
+ }
+ }
+}
+
diff --git a/STALService/src/main/java/at/gv/egiz/stal/service/translator/TranslationException.java b/STALService/src/main/java/at/gv/egiz/stal/service/translator/TranslationException.java
new file mode 100644
index 00000000..fd9da988
--- /dev/null
+++ b/STALService/src/main/java/at/gv/egiz/stal/service/translator/TranslationException.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2008 Federal Chancellery Austria and
+ * Graz University of Technology
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package at.gv.egiz.stal.service.translator;
+
+/**
+ *
+ * @author Clemens Orthacker <clemens.orthacker@iaik.tugraz.at>
+ */
+public class TranslationException extends Exception {
+
+ private Class unknownClass;
+
+ /**
+ * Creates a new instance of <code>TranslationException</code> without detail message.
+ * @param unknownClass the class that could not be translated
+ */
+ public TranslationException(Class unkownClass) {
+ this.unknownClass = unkownClass;
+ }
+
+ @Override
+ public String getMessage() {
+ return "Failed to translate type " + unknownClass;
+ }
+
+
+
+}
diff --git a/STALService/src/main/java/at/gv/egiz/stal/util/STALTranslator.java b/STALService/src/main/java/at/gv/egiz/stal/util/STALTranslator.java
deleted file mode 100644
index 6bbd7301..00000000
--- a/STALService/src/main/java/at/gv/egiz/stal/util/STALTranslator.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
-package at.gv.egiz.stal.util;
-
-import at.gv.egiz.stal.ErrorResponse;
-import at.gv.egiz.stal.InfoboxReadRequest;
-import at.gv.egiz.stal.InfoboxReadResponse;
-import at.gv.egiz.stal.QuitRequest;
-import at.gv.egiz.stal.STALRequest;
-import at.gv.egiz.stal.STALResponse;
-import at.gv.egiz.stal.SignRequest;
-import at.gv.egiz.stal.SignResponse;
-import at.gv.egiz.stal.service.types.*;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.List;
-import javax.xml.bind.JAXBElement;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-
-/**
- *
- * @author clemens
- */
-public class STALTranslator {
-
- protected static final Log log = LogFactory.getLog(STALTranslator.class);
-
- public static List<STALRequest> translateRequests(List<JAXBElement<? extends RequestType>> requests) {
- List<STALRequest> stalRequests = new ArrayList<STALRequest>(requests.size());
- for (JAXBElement<? extends RequestType> requestElt : requests) {
- RequestType request = requestElt.getValue();
- if (request instanceof InfoboxReadRequestType) {
- InfoboxReadRequest stalReq = new InfoboxReadRequest();
- stalReq.setDomainIdentifier(((InfoboxReadRequestType) request).getDomainIdentifier());
- stalReq.setInfoboxIdentifier(((InfoboxReadRequestType) request).getInfoboxIdentifier());
- stalRequests.add(stalReq);
- } else if (request instanceof SignRequestType) {
- SignRequest stalReq = new SignRequest();
- stalReq.setKeyIdentifier(((SignRequestType) request).getKeyIdentifier());
- stalReq.setSignedInfo(((SignRequestType) request).getSignedInfo());
- stalRequests.add(stalReq);
- } else if (request instanceof QuitRequestType) {
- stalRequests.add(new QuitRequest());
- } else {
- log.error("unknown STALService request type: " + request.getClass());
- stalRequests = Collections.singletonList((STALRequest) new QuitRequest());
- break;
- }
- }
- return stalRequests;
- }
-
- public static List<JAXBElement<? extends ResponseType>> fromSTAL(List<STALResponse> stalResponses) {
- ObjectFactory stalObjFactory = new ObjectFactory();
- List<JAXBElement<? extends ResponseType>> responses = new ArrayList<JAXBElement<? extends ResponseType>>(stalResponses.size());
- for (STALResponse stalResp : stalResponses) {
- if (stalResp instanceof InfoboxReadResponse) {
- InfoboxReadResponseType resp = stalObjFactory.createInfoboxReadResponseType();
- resp.setInfoboxValue(((InfoboxReadResponse) stalResp).getInfoboxValue());
- responses.add(stalObjFactory.createGetNextRequestTypeInfoboxReadResponse(resp));
- } else if (stalResp instanceof SignResponse) {
- SignResponseType resp = stalObjFactory.createSignResponseType();
- resp.setSignatureValue(((SignResponse) stalResp).getSignatureValue());
- responses.add(stalObjFactory.createGetNextRequestTypeSignResponse(resp));
- } else if (stalResp instanceof ErrorResponse) {
- ErrorResponseType resp = stalObjFactory.createErrorResponseType();
- resp.setErrorCode(((ErrorResponse) stalResp).getErrorCode());
- resp.setErrorMessage(((ErrorResponse) stalResp).getErrorMessage());
- responses.add(stalObjFactory.createGetNextRequestTypeErrorResponse(resp));
- } else {
- log.error("unknown STAL response type: " + stalResp.getClass());
- ErrorResponseType resp = stalObjFactory.createErrorResponseType();
- resp.setErrorCode(4000);
- resp.setErrorMessage("unknown STAL response type: " + stalResp.getClass());
- responses.clear();
- responses.add(stalObjFactory.createGetNextRequestTypeErrorResponse(resp));
- break;
- }
- }
- return responses;
- }
-
- public static List<STALResponse> toSTAL(List<JAXBElement<? extends ResponseType>> responses) {
- List<STALResponse> stalResponses = new ArrayList<STALResponse>(responses.size());
- for (JAXBElement<? extends ResponseType> respElt : responses) {
- ResponseType resp = respElt.getValue();
- if (resp instanceof InfoboxReadResponseType) {
- InfoboxReadResponse stalResp = new InfoboxReadResponse();
- stalResp.setInfoboxValue(((InfoboxReadResponseType) resp).getInfoboxValue());
- stalResponses.add(stalResp);
- } else if (resp instanceof SignResponseType) {
- SignResponse stalResp = new SignResponse();
- stalResp.setSignatureValue(((SignResponseType) resp).getSignatureValue());
- stalResponses.add(stalResp);
- } else if (resp instanceof ErrorResponseType) {
- ErrorResponse stalResp = new ErrorResponse();
- stalResp.setErrorCode(((ErrorResponseType) resp).getErrorCode());
- stalResp.setErrorMessage(((ErrorResponseType) resp).getErrorMessage());
- stalResponses.add(stalResp);
- } else {
- log.error("unknown STALService response type: " + resp.getClass());
- ErrorResponse stalResp = new ErrorResponse();
- stalResp.setErrorCode(4000);
- stalResp.setErrorMessage("unknown STALService response type: " + resp.getClass());
- stalResponses = Collections.singletonList((STALResponse) stalResp);
- break;
- }
- }
- return stalResponses;
- }
-}