From 212bbffc24ccc1cd909cabdc9650491dd221cd60 Mon Sep 17 00:00:00 2001 From: clemenso Date: Thu, 22 Jan 2009 13:21:50 +0000 Subject: STALTranslator git-svn-id: https://joinup.ec.europa.eu/svn/mocca/trunk@283 8a26b1a7-26f0-462f-b9ef-d0e30c41f5a4 --- BKUApplet/pom.xml | 7 - .../gv/egiz/bku/online/applet/AppletBKUWorker.java | 204 ++++++++------- .../at/gv/egiz/bku/online/applet/BKUApplet.java | 2 +- BKUApplet/src/test/resources/appletTest.html | 2 +- .../main/java/at/gv/egiz/bku/gui/BKUGUIFacade.java | 1 + .../at/gv/egiz/bku/gui/Messages.properties | 1 + .../at/gv/egiz/bku/gui/Messages_en.properties | 3 +- .../stal/service/impl/STALRequestBrokerImpl.java | 109 +++++--- .../gv/egiz/stal/service/impl/STALServiceImpl.java | 4 +- .../stal/service/translator/STALTranslator.java | 274 +++++++++++++++++++++ .../service/translator/TranslationException.java | 42 ++++ .../java/at/gv/egiz/stal/util/STALTranslator.java | 114 --------- .../service/translator/STALTranslatorTest.java | 146 +++++++++++ .../src/test/resources/commons-logging.properties | 1 + 14 files changed, 665 insertions(+), 245 deletions(-) create mode 100644 STALService/src/main/java/at/gv/egiz/stal/service/translator/STALTranslator.java create mode 100644 STALService/src/main/java/at/gv/egiz/stal/service/translator/TranslationException.java delete mode 100644 STALService/src/main/java/at/gv/egiz/stal/util/STALTranslator.java create mode 100644 STALService/src/test/java/at/gv/egiz/stal/service/translator/STALTranslatorTest.java create mode 100644 STALService/src/test/resources/commons-logging.properties diff --git a/BKUApplet/pom.xml b/BKUApplet/pom.xml index f0ee7255..4491ff3a 100644 --- a/BKUApplet/pom.xml +++ b/BKUApplet/pom.xml @@ -104,13 +104,6 @@ - - org.apache.maven.plugins - maven-surefire-plugin - - true - - diff --git a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/AppletBKUWorker.java b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/AppletBKUWorker.java index 3903bf10..3b6d007a 100644 --- a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/AppletBKUWorker.java +++ b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/AppletBKUWorker.java @@ -23,13 +23,14 @@ import at.gv.egiz.stal.STALResponse; import at.gv.egiz.stal.SignRequest; import at.gv.egiz.stal.service.STALPortType; import at.gv.egiz.stal.service.STALService; +import at.gv.egiz.stal.service.translator.STALTranslator; +import at.gv.egiz.stal.service.translator.TranslationException; import at.gv.egiz.stal.service.types.ErrorResponseType; import at.gv.egiz.stal.service.types.GetNextRequestResponseType; import at.gv.egiz.stal.service.types.GetNextRequestType; import at.gv.egiz.stal.service.types.ObjectFactory; import at.gv.egiz.stal.service.types.RequestType; import at.gv.egiz.stal.service.types.ResponseType; -import at.gv.egiz.stal.util.STALTranslator; import java.applet.AppletContext; import java.net.MalformedURLException; import java.net.URL; @@ -37,6 +38,7 @@ import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; +import javax.xml.ws.WebServiceException; /** * @@ -49,6 +51,7 @@ public class AppletBKUWorker extends AbstractBKUWorker implements Runnable { protected String sessionId; protected STALPortType stalPort; private ObjectFactory stalObjFactory = new ObjectFactory(); + private STALTranslator translator = new STALTranslator(); public AppletBKUWorker(BKUGUIFacade gui, AppletContext ctx, AppletParameterProvider paramProvider) { @@ -72,108 +75,147 @@ public class AppletBKUWorker extends AbstractBKUWorker implements Runnable { @Override public void run() { gui.showWelcomeDialog(); + try { stalPort = getSTALPort(); - } catch (Exception e) { - log.fatal("Failed to get STAL web-service port: " + e.getMessage(), e); - actionCommandList.clear(); - actionCommandList.add("ok"); - gui.showErrorDialog(BKUGUIFacade.ERR_SERVICE_UNREACHABLE, - new Object[]{e.getMessage()}); - try { - waitForAction(); - } catch (InterruptedException e1) { - log.error(e1); - } - return; - } - try { - registerSignRequestHandler(); + registerSignRequestHandler(stalPort, sessionId); GetNextRequestResponseType nextRequestResp = stalPort.connect(sessionId); + do { - List> requests = nextRequestResp.getInfoboxReadRequestOrSignRequestOrQuitRequest(); + List> requests; + List> responses = new ArrayList>(); - // (rather use validator) - if (requests.size() == 0) { - log.error("Received empty NextRequestResponse: no STAL requests to handle. (STAL-X requests might not have gotten unmarshalled)"); - throw new Exception("No STAL requests to handle."); - } + try { + requests = nextRequestResp.getInfoboxReadRequestOrSignRequestOrQuitRequest(); + responses.clear(); - List stalRequests = STALTranslator.translateRequests(requests); + // (rather use validator) + if (requests.size() == 0) { + log.error("Received empty NextRequestResponse: no STAL requests to handle. (STAL-X requests might not have gotten unmarshalled)"); + throw new RuntimeException("No STAL requests to handle."); + } - if (log.isInfoEnabled()) { - StringBuilder sb = new StringBuilder("Received "); - sb.append(stalRequests.size()); - sb.append(" STAL requests: "); - for (STALRequest r : stalRequests) { - sb.append(r.getClass()); - sb.append(' '); + if (log.isInfoEnabled()) { + StringBuilder sb = new StringBuilder("Received "); + sb.append(requests.size()); + sb.append(" requests: "); + for (JAXBElement r : requests) { + sb.append(r.getValue().getClass()); + sb.append(' '); + } + log.info(sb.toString()); } - log.info(sb.toString()); - } - boolean handle = true; - for (STALRequest request : stalRequests) { - if (request instanceof at.gv.egiz.stal.InfoboxReadRequest) { - at.gv.egiz.stal.InfoboxReadRequest r = (at.gv.egiz.stal.InfoboxReadRequest) request; - String infoboxId = r.getInfoboxIdentifier(); - String domainId = r.getDomainIdentifier(); - if ("IdentityLink".equals(infoboxId) && domainId == null) { - if (!InternalSSLSocketFactory.getInstance().isEgovAgency()) { - handle = false; - } + List stalRequests = new ArrayList(); + for (JAXBElement req : requests) { + try { + stalRequests.add(translator.translate(req)); + } catch (TranslationException ex) { + log.error("Received unknown request from server STAL: " + ex.getMessage()); + throw new RuntimeException(ex); } } - } - List> responses; - if (handle) { + checkPermission(stalRequests); + List stalResponses = handleRequest(stalRequests); - if (log.isInfoEnabled()) { - StringBuilder sb = new StringBuilder(stalResponses.size()); - sb.append(" STAL responses: "); - for (STALResponse r : stalResponses) { - sb.append(r.getClass()); - sb.append(' '); + for (STALResponse stalResponse : stalResponses) { + try { + responses.add(translator.translate(stalResponse)); + } catch (TranslationException ex) { + log.error("Received unknown response from STAL: " + ex.getMessage()); + throw new RuntimeException(ex); } - log.info(sb.toString()); } - responses = STALTranslator.fromSTAL(stalResponses); - } else { - log.error("Insufficient rights to execute command InfoboxReadRequest for Infobox IdentityLink, return Error 6002"); - responses = new ArrayList>(1); + + } catch (RuntimeException ex) { + // return ErrorResponse to server, which displays error page + log.error(ex.getMessage()); + Throwable cause = ex.getCause(); ErrorResponseType err = stalObjFactory.createErrorResponseType(); - err.setErrorCode(6002); - // err.setErrorMessage(); + if (cause != null) { + log.error("caused by: " + cause.getMessage()); + if (cause instanceof SecurityException) { + err.setErrorCode(6002); + } else { + err.setErrorCode(4000); + } + } else { + err.setErrorCode(4000); + } + responses.clear(); responses.add(stalObjFactory.createGetNextRequestTypeErrorResponse(err)); - } - if (!finished) { - log.info("Not finished yet (BKUWorker: " + this + "), sending responses"); - GetNextRequestType nextRequest = stalObjFactory.createGetNextRequestType(); - nextRequest.setSessionId(sessionId); - nextRequest.getInfoboxReadResponseOrSignResponseOrErrorResponse().addAll(responses); - nextRequestResp = stalPort.getNextRequest(nextRequest); + } finally { + if (!finished) { + if (log.isInfoEnabled()) { + StringBuilder sb = new StringBuilder("Sending "); + sb.append(responses.size()); + sb.append(" responses: "); + for (JAXBElement r : responses) { + sb.append(r.getValue().getClass()); + sb.append(' '); + } + log.info(sb.toString()); + } + GetNextRequestType nextRequest = stalObjFactory.createGetNextRequestType(); + nextRequest.setSessionId(sessionId); + nextRequest.getInfoboxReadResponseOrSignResponseOrErrorResponse().addAll(responses); + nextRequestResp = stalPort.getNextRequest(nextRequest); + } } + + } while (!finished); log.info("Done " + Thread.currentThread().getName()); + + } catch (WebServiceException ex) { + log.fatal("communication error with server STAL: " + ex.getMessage(), ex); + showErrorDialog(BKUGUIFacade.ERR_SERVICE_UNREACHABLE, ex); + } catch (MalformedURLException ex) { + log.fatal(ex.getMessage(), ex); + showErrorDialog(BKUGUIFacade.ERR_CONFIG, ex); } catch (Exception ex) { log.error(ex.getMessage(), ex); - gui.showErrorDialog(BKUGUIFacade.ERR_UNKNOWN, new Object[]{ex.getMessage()}); - try { - waitForAction(); - } catch (InterruptedException e) { - log.error(e); - } + showErrorDialog(BKUGUIFacade.ERR_UNKNOWN, ex); + } finally { if (signatureCard != null) { signatureCard.disconnect(false); } } + sendRedirect(); } + private void checkPermission(List stalRequests) { + for (STALRequest request : stalRequests) { + if (request instanceof at.gv.egiz.stal.InfoboxReadRequest) { + at.gv.egiz.stal.InfoboxReadRequest r = (at.gv.egiz.stal.InfoboxReadRequest) request; + String infoboxId = r.getInfoboxIdentifier(); + String domainId = r.getDomainIdentifier(); + if ("IdentityLink".equals(infoboxId) && domainId == null) { + if (!InternalSSLSocketFactory.getInstance().isEgovAgency()) { + throw new RuntimeException(new SecurityException("Insufficient rights to execute command InfoboxReadRequest for Infobox IdentityLink")); + } + } + } + } + } + + private void showErrorDialog(String err_code, Exception ex) { + actionCommandList.clear(); + actionCommandList.add("ok"); + gui.showErrorDialog(err_code, + new Object[]{ex.getMessage()}, this, "ok"); + try { + waitForAction(); + } catch (InterruptedException e) { + log.error(e); + } + } + protected void sendRedirect() { try { URL redirectURL = params.getURLParameter(BKUApplet.REDIRECT_URL, @@ -201,20 +243,10 @@ public class AppletBKUWorker extends AbstractBKUWorker implements Runnable { return stal.getSTALPort(); } - private void registerSignRequestHandler() throws MalformedURLException { - String hashDataDisplayStyle = params.getAppletParameter(BKUApplet.HASHDATA_DISPLAY); - if (BKUApplet.HASHDATA_DISPLAY_BROWSER.equals(hashDataDisplayStyle)) { - URL hashDataURL = params.getURLParameter(BKUApplet.HASHDATA_URL, - sessionId); - log.debug("register SignRequestHandler for HashDataURL " + hashDataURL); - addRequestHandler(SignRequest.class, new BrowserHashDataDisplay(ctx, - hashDataURL)); - } else { - // BKUApplet.HASHDATA_DISPLAY_FRAME - log.debug("register SignRequestHandler for STAL port " + BKUApplet.WSDL_URL); - AppletHashDataDisplay handler = new AppletHashDataDisplay(stalPort, - sessionId); - addRequestHandler(SignRequest.class, handler); - } + private void registerSignRequestHandler(STALPortType stalPort, String sessionId) { + log.debug("register SignRequestHandler (resolve hashdata via STAL Webservice)"); + AppletHashDataDisplay handler = new AppletHashDataDisplay(stalPort, + sessionId); + addRequestHandler(SignRequest.class, handler); } } diff --git a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java index d0f6d489..41dae3ba 100644 --- a/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java +++ b/BKUApplet/src/main/java/at/gv/egiz/bku/online/applet/BKUApplet.java @@ -53,7 +53,7 @@ public class BKUApplet extends JApplet implements AppletParameterProvider { public static final String REDIRECT_URL = "RedirectURL"; public static final String REDIRECT_TARGET = "RedirectTarget"; // public static final String HASHDATA_DISPLAY_INTERNAL = "internal"; - public static final String HASHDATA_DISPLAY_BROWSER = "browser"; +// public static final String HASHDATA_DISPLAY_BROWSER = "browser"; public static final String HASHDATA_DISPLAY_FRAME = "frame"; /** diff --git a/BKUApplet/src/test/resources/appletTest.html b/BKUApplet/src/test/resources/appletTest.html index b3f68a91..2bf9ba0c 100644 --- a/BKUApplet/src/test/resources/appletTest.html +++ b/BKUApplet/src/test/resources/appletTest.html @@ -18,7 +18,7 @@
diff --git a/BKUCommonGUI/src/main/java/at/gv/egiz/bku/gui/BKUGUIFacade.java b/BKUCommonGUI/src/main/java/at/gv/egiz/bku/gui/BKUGUIFacade.java index d3cb76f2..5ee0058f 100644 --- a/BKUCommonGUI/src/main/java/at/gv/egiz/bku/gui/BKUGUIFacade.java +++ b/BKUCommonGUI/src/main/java/at/gv/egiz/bku/gui/BKUGUIFacade.java @@ -37,6 +37,7 @@ public interface BKUGUIFacade { public static final String ERR_CARD_NOTACTIVATED = "error.card.notactivated"; public static final String ERR_VIEWER = "error.viewer"; public static final String ERR_EXTERNAL_LINK = "error.external.link"; + public static final String ERR_CONFIG = "error.config"; public static final String MESSAGES_BUNDLE = "at/gv/egiz/bku/gui/Messages"; public static final String DEFAULT_BACKGROUND = "/images/BackgroundChipperling.png"; diff --git a/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages.properties b/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages.properties index e560ca98..d2b47a9d 100644 --- a/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages.properties +++ b/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages.properties @@ -72,6 +72,7 @@ error.card.locked=B\u00FCrgerkarte ist gesperrt error.card.notactivated=B\u00FCrgerkartenfunktion ist nicht aktiviert error.viewer=Der Inhalt kann nicht dargestellt werden: {0} error.external.link=Externer Link {0} wird nicht ge\u00F6ffnet +error.config=Fehlerhafte Konfiguration des Systems: {0} # Help Topics help.welcome=Startseite diff --git a/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages_en.properties b/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages_en.properties index 96f99a8a..524a2a98 100644 --- a/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages_en.properties +++ b/BKUCommonGUI/src/main/resources/at/gv/egiz/bku/gui/Messages_en.properties @@ -68,7 +68,8 @@ error.test=Error1 {0} - Error2 {1} error.card.locked=Citizen card is locked error.card.notactivated=Citizen card not activated error.viewer=Failed to display contents: {0} -error.external.link=Externer Link {0} wird nicht ge\u00F6ffnet +error.external.link=Cannot open external link {0} +error.config=Incorrect system configuration: {0} # Help Topics help.welcome=Welcome page diff --git a/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALRequestBrokerImpl.java b/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALRequestBrokerImpl.java index 252327d3..a617c61f 100644 --- a/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALRequestBrokerImpl.java +++ b/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALRequestBrokerImpl.java @@ -24,13 +24,14 @@ 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.service.translator.STALTranslator; +import at.gv.egiz.stal.service.translator.TranslationException; import at.gv.egiz.stal.service.types.InfoboxReadRequestType; 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.util.STALTranslator; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -54,7 +55,8 @@ public class STALRequestBrokerImpl implements STALRequestBroker { private static final Log log = LogFactory.getLog(STALRequestBrokerImpl.class); - private ObjectFactory stalObjFactory = new ObjectFactory(); + private ObjectFactory of = new ObjectFactory(); + private STALTranslator translator = new STALTranslator(); private boolean interrupted = false; @@ -69,6 +71,7 @@ public class STALRequestBrokerImpl implements STALRequestBroker { if (timeoutMillisec <= 0) timeoutMillisec = DEFAULT_TIMEOUT_MS; timeout = timeoutMillisec; +// translator.registerTranslationHandler(handler); requests = new ArrayList>(); responses = new ArrayList>(); hashDataInputs = new ArrayList(); @@ -97,42 +100,70 @@ public class STALRequestBrokerImpl implements STALRequestBroker { hashDataInputs.clear(); for (STALRequest stalRequest : stalRequests) { + try { + JAXBElement request = translator.translate(stalRequest); + requests.add(request); if (stalRequest instanceof SignRequest) { - log.trace("Received SignRequest, keep HashDataInput."); - SignRequestType reqT = stalObjFactory.createSignRequestType(); - reqT.setKeyIdentifier(((SignRequest) stalRequest).getKeyIdentifier()); - reqT.setSignedInfo(((SignRequest) stalRequest).getSignedInfo()); - JAXBElement req = stalObjFactory.createGetNextRequestResponseTypeSignRequest(reqT); - requests.add(req); - //DataObjectHashDataInput with reference caching enabled DataObject + //TODO refactor SignRequestType to keep HDI + // and getHashDataInput() accesses request obj + // (requests are cleared only when we receive the response) + // DataObjectHashDataInput with reference caching enabled DataObject hashDataInputs.addAll(((SignRequest) stalRequest).getHashDataInput()); - break; - } else if (stalRequest instanceof InfoboxReadRequest) { - log.trace("Received InfoboxReadRequest"); - InfoboxReadRequestType reqT = new InfoboxReadRequestType(); - reqT.setInfoboxIdentifier(((InfoboxReadRequest) stalRequest).getInfoboxIdentifier()); - reqT.setDomainIdentifier(((InfoboxReadRequest) stalRequest).getDomainIdentifier()); - JAXBElement req = stalObjFactory.createGetNextRequestResponseTypeInfoboxReadRequest(reqT); - requests.add(req); } else if (stalRequest instanceof QuitRequest) { log.trace("Received QuitRequest, do not wait for responses."); - QuitRequestType reqT = stalObjFactory.createQuitRequestType(); - JAXBElement req = stalObjFactory.createGetNextRequestResponseTypeQuitRequest(reqT); - requests.add(req); - log.trace("notifying request consumers"); - requests.notify(); - return new ArrayList(); - } else { - log.error("Received unsupported STAL request: " + stalRequest.getClass().getName() + ", send QUIT"); - requests.clear(); - QuitRequestType reqT = stalObjFactory.createQuitRequestType(); - JAXBElement req = stalObjFactory.createGetNextRequestResponseTypeQuitRequest(reqT); - requests.add(req); log.trace("notifying request consumers"); requests.notify(); return new ArrayList(); } + } catch (TranslationException ex) { + log.error(ex.getMessage() + ", send QUIT"); + requests.clear(); + QuitRequestType reqT = of.createQuitRequestType(); + JAXBElement req = of.createGetNextRequestResponseTypeQuitRequest(reqT); + requests.add(req); + log.trace("notifying request consumers"); + requests.notify(); + return new ArrayList(); + } } + + +// if (stalRequest instanceof SignRequest) { +// log.trace("Received SignRequest, keep HashDataInput."); +// SignRequestType reqT = of.createSignRequestType(); +// reqT.setKeyIdentifier(((SignRequest) stalRequest).getKeyIdentifier()); +// reqT.setSignedInfo(((SignRequest) stalRequest).getSignedInfo()); +// JAXBElement req = of.createGetNextRequestResponseTypeSignRequest(reqT); +// requests.add(req); +// //DataObjectHashDataInput with reference caching enabled DataObject +// hashDataInputs.addAll(((SignRequest) stalRequest).getHashDataInput()); +// break; +// } else if (stalRequest instanceof InfoboxReadRequest) { +// log.trace("Received InfoboxReadRequest"); +// InfoboxReadRequestType reqT = new InfoboxReadRequestType(); +// reqT.setInfoboxIdentifier(((InfoboxReadRequest) stalRequest).getInfoboxIdentifier()); +// reqT.setDomainIdentifier(((InfoboxReadRequest) stalRequest).getDomainIdentifier()); +// JAXBElement req = of.createGetNextRequestResponseTypeInfoboxReadRequest(reqT); +// requests.add(req); +// } else if (stalRequest instanceof QuitRequest) { +// log.trace("Received QuitRequest, do not wait for responses."); +// QuitRequestType reqT = of.createQuitRequestType(); +// JAXBElement req = of.createGetNextRequestResponseTypeQuitRequest(reqT); +// requests.add(req); +// log.trace("notifying request consumers"); +// requests.notify(); +// return new ArrayList(); +// } else { +// log.error("Received unsupported STAL request: " + stalRequest.getClass().getName() + ", send QUIT"); +// requests.clear(); +// QuitRequestType reqT = of.createQuitRequestType(); +// JAXBElement req = of.createGetNextRequestResponseTypeQuitRequest(reqT); +// requests.add(req); +// log.trace("notifying request consumers"); +// requests.notify(); +// return new ArrayList(); +// } +// } log.trace("notifying request consumers"); requests.notify(); } @@ -150,12 +181,24 @@ public class STALRequestBrokerImpl implements STALRequestBroker { } } log.trace("consuming responses"); - List resps = STALTranslator.toSTAL(responses); + List stalResponses = new ArrayList(); + try { + for (JAXBElement resp : responses) { + STALResponse stalResp = translator.translate(resp); + stalResponses.add(stalResp); + } + } catch (TranslationException ex) { + log.error(ex.getMessage() + ", return ErrorResponse (4000)"); + ErrorResponse stalResp = new ErrorResponse(4000); + stalResp.setErrorMessage(ex.getMessage()); + stalResponses = Collections.singletonList((STALResponse) stalResp); + } + responses.clear(); log.trace("notifying response producers"); responses.notify(); - return resps; + return stalResponses; } } catch (InterruptedException ex) { log.warn("interrupt in handleRequest(): " + ex.getMessage()); @@ -277,8 +320,8 @@ public class STALRequestBrokerImpl implements STALRequestBroker { } private List> createSingleQuitRequest() { - QuitRequestType quitT = stalObjFactory.createQuitRequestType(); - JAXBElement quit = stalObjFactory.createGetNextRequestResponseTypeQuitRequest(quitT); + QuitRequestType quitT = of.createQuitRequestType(); + JAXBElement quit = of.createGetNextRequestResponseTypeQuitRequest(quitT); ArrayList> l = new ArrayList>(); l.add(quit); return l; diff --git a/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALServiceImpl.java b/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALServiceImpl.java index 45629869..afeba9cb 100644 --- a/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALServiceImpl.java +++ b/BKUOnline/src/main/java/at/gv/egiz/stal/service/impl/STALServiceImpl.java @@ -147,8 +147,8 @@ public class STALServiceImpl implements STALPortType { sb.append("] containing "); sb.append(responsesIn.size()); sb.append(" responses: "); - for (Object respIn : responsesIn) { - sb.append(respIn.getClass()); + for (JAXBElement respIn : responsesIn) { + sb.append(respIn.getValue().getClass()); sb.append(' '); } log.debug(sb.toString()); 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 + */ +public class STALTranslator { + + private static final Log log = LogFactory.getLog(STALTranslator.class); + protected Map handlerMap = new HashMap(); + + 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 translate(STALRequest request) throws TranslationException { + if (handlerMap.containsKey(request.getClass())) { + TranslationHandler handler = handlerMap.get(request.getClass()); + JAXBElement 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 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 translate(STALResponse response) throws TranslationException { + if (handlerMap.containsKey(response.getClass())) { + TranslationHandler handler = handlerMap.get(response.getClass()); + JAXBElement 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 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 getSupportedTypes(); + + JAXBElement translate(STALRequest request) throws TranslationException; + + STALRequest translate(RequestType request) throws TranslationException; + + JAXBElement 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 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 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 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 + */ +public class TranslationException extends Exception { + + private Class unknownClass; + + /** + * Creates a new instance of TranslationException 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 translateRequests(List> requests) { - List stalRequests = new ArrayList(requests.size()); - for (JAXBElement 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> fromSTAL(List stalResponses) { - ObjectFactory stalObjFactory = new ObjectFactory(); - List> responses = new ArrayList>(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 toSTAL(List> responses) { - List stalResponses = new ArrayList(responses.size()); - for (JAXBElement 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; - } -} diff --git a/STALService/src/test/java/at/gv/egiz/stal/service/translator/STALTranslatorTest.java b/STALService/src/test/java/at/gv/egiz/stal/service/translator/STALTranslatorTest.java new file mode 100644 index 00000000..1dad7973 --- /dev/null +++ b/STALService/src/test/java/at/gv/egiz/stal/service/translator/STALTranslatorTest.java @@ -0,0 +1,146 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ + +package at.gv.egiz.stal.service.translator; + +import at.gv.egiz.stal.STALRequest; +import at.gv.egiz.stal.STALResponse; +import at.gv.egiz.stal.SignRequest; +import at.gv.egiz.stal.service.translator.STALTranslator.TranslationHandler; +import at.gv.egiz.stal.service.types.ObjectFactory; +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 javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import static org.junit.Assert.*; + +/** + * + * @author clemens + */ +public class STALTranslatorTest { + + static ObjectFactory of; + + public STALTranslatorTest() { + } + + @BeforeClass + public static void setUpClass() throws Exception { + of = new ObjectFactory(); + } + + @AfterClass + public static void tearDownClass() throws Exception { + } + + @Before + public void setUp() { + } + + @After + public void tearDown() { + } + + /** + * Test of registerTranslationHandler method, of class STALTranslator. + */ + @Test + @Ignore + public void testRegisterTranslationHandler() { + System.out.println("registerTranslationHandler"); + TranslationHandler handler = null; + STALTranslator instance = new STALTranslator(); + instance.registerTranslationHandler(handler); + + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of translate method, of class STALTranslator. + */ + @Test + public void testTranslate_STALRequest() throws Exception { + System.out.println("translate"); + SignRequest request = new SignRequest(); + request.setKeyIdentifier("kid"); + request.setSignedInfo("signedinfo".getBytes()); + STALTranslator instance = new STALTranslator(); + JAXBElement result = instance.translate(request); + assertEquals(SignRequestType.class, result.getValue().getClass()); + SignRequestType resultT = (SignRequestType) result.getValue(); + assertEquals(request.getKeyIdentifier(), resultT.getKeyIdentifier()); + assertEquals(request.getSignedInfo(), resultT.getSignedInfo()); + } + + /** + * Test of translate method, of class STALTranslator. + */ + @Test + public void testTranslate_1args_1() throws Exception { + System.out.println("translate"); + SignRequestType req = of.createSignRequestType(); + req.setKeyIdentifier("kid"); + req.setSignedInfo("signedinfo".getBytes()); + JAXBElement request = of.createGetNextRequestResponseTypeSignRequest(req); + STALTranslator instance = new STALTranslator(); + STALRequest result = instance.translate(request); + assertEquals(SignRequest.class, result.getClass()); + assertEquals(req.getKeyIdentifier(), ((SignRequest) result).getKeyIdentifier()); + assertEquals(req.getSignedInfo(), ((SignRequest) result).getSignedInfo()); + } + + @Test(expected=RuntimeException.class) + public void testTranslate_invalidInput() throws Exception { + System.out.println("translate"); + QName n = new QName("http://www.egiz.gv.at/stal", "SignRequest"); + JAXBElement request = new JAXBElement(n, SignRequestType.class, null); + STALTranslator instance = new STALTranslator(); + STALRequest result = instance.translate(request); + assertEquals(SignRequest.class, result.getClass()); + } + + + /** + * Test of translate method, of class STALTranslator. + */ + @Test + @Ignore + public void testTranslate_STALResponse() throws Exception { + System.out.println("translate"); + STALResponse response = null; + STALTranslator instance = new STALTranslator(); + JAXBElement expResult = null; + JAXBElement result = instance.translate(response); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + + /** + * Test of translate method, of class STALTranslator. + */ + @Test + @Ignore + public void testTranslate_1args_2() throws Exception { + System.out.println("translate"); + JAXBElement response = null; + STALTranslator instance = new STALTranslator(); + STALResponse expResult = null; + STALResponse result = instance.translate(response); + assertEquals(expResult, result); + // TODO review the generated test code and remove the default call to fail. + fail("The test case is a prototype."); + } + +} \ No newline at end of file diff --git a/STALService/src/test/resources/commons-logging.properties b/STALService/src/test/resources/commons-logging.properties new file mode 100644 index 00000000..29292562 --- /dev/null +++ b/STALService/src/test/resources/commons-logging.properties @@ -0,0 +1 @@ +org.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger -- cgit v1.2.3