Révision 96454bcd
Ajouté par Sylvain Sauvage il y a plus de 11 ans
OMeGa/OmICron/src/main/java/fr/unicaen/iota/application/soap/client/OmICron.java | ||
---|---|---|
/*
|
||
* This program is a part of the IoTa project.
|
||
*
|
||
* Copyright © 2008-2012 Université de Caen Basse-Normandie, GREYC
|
||
*
|
||
* Copyright © 2012 Université de Caen Basse-Normandie, GREYC
|
||
*
|
||
* This program is free software: you can redistribute it and/or modify
|
||
* it under the terms of the GNU General Public License as published by
|
||
* the Free Software Foundation, either version 3 of the License, or
|
||
... | ... | |
*/
|
||
package fr.unicaen.iota.application.soap.client;
|
||
|
||
import java.rmi.RemoteException;
|
||
import org.apache.axis2.AxisFault;
|
||
import fr.unicaen.iota.application.model.*;
|
||
import fr.unicaen.iota.application.soap.IoTaException;
|
||
import fr.unicaen.iota.application.soap.IoTaService;
|
||
import fr.unicaen.iota.application.soap.IoTaServicePortType;
|
||
import fr.unicaen.iota.ds.model.TEventItem;
|
||
import fr.unicaen.iota.ds.model.TServiceType;
|
||
import fr.unicaen.iota.mu.EPCISEventTypeHelper;
|
||
import fr.unicaen.iota.nu.ONSEntryType;
|
||
import fr.unicaen.iota.tau.model.Identity;
|
||
import java.io.File;
|
||
import java.io.FileInputStream;
|
||
import java.net.URL;
|
||
import java.security.KeyStore;
|
||
import java.security.SecureRandom;
|
||
import java.security.cert.CertificateException;
|
||
import java.security.cert.X509Certificate;
|
||
import java.util.EnumMap;
|
||
import java.util.List;
|
||
import java.util.Map;
|
||
import javax.net.ssl.KeyManagerFactory;
|
||
import javax.net.ssl.TrustManager;
|
||
import javax.net.ssl.X509TrustManager;
|
||
import org.apache.commons.logging.Log;
|
||
import org.apache.commons.logging.LogFactory;
|
||
import org.apache.cxf.configuration.jsse.TLSClientParameters;
|
||
import org.apache.cxf.endpoint.Client;
|
||
import org.apache.cxf.frontend.ClientProxy;
|
||
import org.apache.cxf.transport.http.HTTPConduit;
|
||
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
|
||
import org.fosstrak.epcis.model.EPC;
|
||
import org.fosstrak.epcis.model.EPCISEventType;
|
||
import org.fosstrak.epcis.model.QueryParam;
|
||
import org.fosstrak.epcis.model.QueryParams;
|
||
|
||
/**
|
||
*
|
||
*/
|
||
public class OmICron {
|
||
public class OmICron implements X509TrustManager {
|
||
|
||
private Identity identity;
|
||
private IoTaServicePortType port;
|
||
private static final Log log = LogFactory.getLog(OmICron.class);
|
||
|
||
public OmICron(Identity id, String oAddress) {
|
||
this(id, oAddress, null, null);
|
||
}
|
||
|
||
public OmICron(Identity id, String address, String pksFilename, String pksPassword) {
|
||
log.trace("new OmICron: " + id + " @ " + address);
|
||
this.identity = id;
|
||
// TODO: TLS
|
||
try {
|
||
configureService(address, pksFilename, pksPassword);
|
||
} catch (Exception e) {
|
||
throw new RuntimeException("Can’t configure service: " + e.getMessage(), e);
|
||
}
|
||
}
|
||
|
||
// TODO: TLS
|
||
public void configureService(String address, String pksFilename, String pksPassword) throws Exception {
|
||
URL wsdlUrl = new URL(address + "?wsdl");
|
||
IoTaService service = new IoTaService(wsdlUrl);
|
||
port = service.getPort(IoTaServicePortType.class);
|
||
|
||
// turn off chunked transfer encoding
|
||
Client client = ClientProxy.getClient(port);
|
||
HTTPConduit httpConduit = (HTTPConduit) client.getConduit();
|
||
HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
|
||
httpClientPolicy.setAllowChunking(false);
|
||
httpConduit.setClient(httpClientPolicy);
|
||
|
||
// TODO: TLS
|
||
if (pksFilename != null) {
|
||
//log.debug("Authenticating with certificate in file: " + pksFilename);
|
||
|
||
if (!wsdlUrl.getProtocol().equalsIgnoreCase("https")) {
|
||
throw new Exception("Authentication method requires the use of HTTPS");
|
||
}
|
||
|
||
public static void main(String[] arg) throws AxisFault, RemoteException {
|
||
String service = "http://localhost:8080/omega/services/IOTA_Service/";
|
||
KeyStore keyStore = KeyStore.getInstance(pksFilename.endsWith(".p12") ? "PKCS12" : "JKS");
|
||
keyStore.load(new FileInputStream(new File(pksFilename)), pksPassword.toCharArray());
|
||
KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
|
||
keyManagerFactory.init(keyStore, pksPassword.toCharArray());
|
||
|
||
TLSClientParameters tlscp = new TLSClientParameters();
|
||
tlscp.setKeyManagers(keyManagerFactory.getKeyManagers());
|
||
tlscp.setSecureRandom(new SecureRandom());
|
||
tlscp.setDisableCNCheck(true);
|
||
tlscp.setTrustManagers(new TrustManager[]{this});
|
||
|
||
httpConduit.setTlsClientParameters(tlscp);
|
||
}
|
||
}
|
||
|
||
public Identity getIdentity() {
|
||
return identity;
|
||
}
|
||
|
||
public void setIdentity(Identity id) {
|
||
this.identity = id;
|
||
}
|
||
|
||
public Map<ONSEntryType, String> queryONS(String epc) throws IoTaException {
|
||
QueryONSRequest in = new QueryONSRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
QueryONSResponse out = port.queryONS(in);
|
||
Map<ONSEntryType, String> res = new EnumMap<ONSEntryType, String>(ONSEntryType.class);
|
||
for (OnsEntry entry : out.getOnsMap()) {
|
||
ONSEntryType key = ONSEntryType.valueOf(entry.getKey());
|
||
res.put(key, entry.getValue());
|
||
}
|
||
return res;
|
||
}
|
||
|
||
public String getReferentDS(String epc) throws IoTaException {
|
||
GetReferentDSRequest in = new GetReferentDSRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
GetReferentDSResponse out = port.getReferentDS(in);
|
||
return out.getUrl();
|
||
}
|
||
|
||
public List<EPCISEventType> traceEPC(String epc) throws IoTaException {
|
||
TraceEPCRequest in = new TraceEPCRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
in.setIdentity(identity);
|
||
TraceEPCResponse out = port.traceEPC(in);
|
||
return EPCISEventTypeHelper.listFromEventList(out.getEventList());
|
||
}
|
||
|
||
private QueryParams createQueryParams(Map<String, String> filters) {
|
||
QueryParams queryParams = new QueryParams();
|
||
for (Map.Entry<String, String> entry : filters.entrySet()) {
|
||
QueryParam qp = new QueryParam();
|
||
qp.setName(entry.getKey());
|
||
qp.setValue(entry.getValue());
|
||
queryParams.getParam().add(qp);
|
||
}
|
||
return queryParams;
|
||
}
|
||
|
||
public List<EPCISEventType> traceEPC(String epc, Map<String, String> filters) throws IoTaException {
|
||
TraceEPCRequest in = new TraceEPCRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
in.setIdentity(identity);
|
||
in.setFilters(createQueryParams(filters));
|
||
TraceEPCResponse out = port.traceEPC(in);
|
||
return EPCISEventTypeHelper.listFromEventList(out.getEventList());
|
||
}
|
||
|
||
public String getEPCDocURL(String epc) throws IoTaException {
|
||
GetEPCDocURLRequest in = new GetEPCDocURLRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
GetEPCDocURLResponse out = port.getEPCDocURL(in);
|
||
return out.getUrl();
|
||
}
|
||
|
||
public List<EPCISEventType> queryEPCIS(String epc, String EPCISAddress) throws IoTaException {
|
||
QueryEPCISRequest in = new QueryEPCISRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
in.setEPCISAddress(EPCISAddress);
|
||
in.setIdentity(identity);
|
||
QueryEPCISResponse out = port.queryEPCIS(in);
|
||
return EPCISEventTypeHelper.listFromEventList(out.getEventList());
|
||
}
|
||
|
||
public List<EPCISEventType> queryEPCIS(Map<String, String> filters, String EPCISAddress) throws IoTaException {
|
||
QueryEPCISRequest in = new QueryEPCISRequest();
|
||
in.setFilters(createQueryParams(filters));
|
||
in.setEPCISAddress(EPCISAddress);
|
||
in.setIdentity(identity);
|
||
QueryEPCISResponse out = port.queryEPCIS(in);
|
||
return EPCISEventTypeHelper.listFromEventList(out.getEventList());
|
||
}
|
||
|
||
public List<TEventItem> queryDS(String epc, String DSAddress) throws IoTaException {
|
||
QueryDSRequest in = new QueryDSRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
in.setIdentity(identity);
|
||
in.setDSAddress(DSAddress);
|
||
QueryDSResponse out = port.queryDS(in);
|
||
return out.getEventList().getEvent();
|
||
}
|
||
|
||
public List<TEventItem> queryDS(String epc, String DSAddress, TServiceType serviceType) throws IoTaException {
|
||
QueryDSRequest in = new QueryDSRequest();
|
||
EPC tepc = new EPC();
|
||
tepc.setValue(epc);
|
||
in.setEpc(tepc);
|
||
in.setIdentity(identity);
|
||
in.setDSAddress(DSAddress);
|
||
in.setServiceType(serviceType);
|
||
QueryDSResponse out = port.queryDS(in);
|
||
return out.getEventList().getEvent();
|
||
}
|
||
|
||
@Override
|
||
public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException {
|
||
throw new UnsupportedOperationException("Not supported yet.");
|
||
}
|
||
|
||
@Override
|
||
public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException {
|
||
throw new UnsupportedOperationException("Not supported yet.");
|
||
}
|
||
|
||
@Override
|
||
public X509Certificate[] getAcceptedIssuers() {
|
||
throw new UnsupportedOperationException("Not supported yet.");
|
||
}
|
||
|
||
public static void main(String args[]) throws Exception {
|
||
String serviceURL = "http://localhost:8080/omega";
|
||
String sid = "anonymous";
|
||
String epc = "urn:epc:id:sgtin:40000.00002.1298283877319";
|
||
if (arg.length != 2) {
|
||
System.err.println("Usage: OmICron <OMeGa Web Service URL> <EPC URN ID>");
|
||
if (args.length != 3) {
|
||
System.err.println("Usage: OmICron <OMeGa Web Service URL> <IDENTITY> <EPC URN ID>");
|
||
System.err.println();
|
||
System.err.println("example: OmICron " + service + " " + epc);
|
||
System.err.println("example: OmICron " + serviceURL + " " + sid + " " + epc);
|
||
System.exit(-1);
|
||
} else {
|
||
service = arg[0];
|
||
epc = arg[1];
|
||
serviceURL = args[0];
|
||
sid = args[1];
|
||
epc = args[2];
|
||
}
|
||
IOTA_ServiceStub iota_ServiceStub = new IOTA_ServiceStub(service);
|
||
System.out.println("Processing hello ...");
|
||
IOTA_ServiceStub.HelloRequest hello = new IOTA_ServiceStub.HelloRequest();
|
||
hello.setHelloRequest(new IOTA_ServiceStub.HelloRequestIn());
|
||
IOTA_ServiceStub.HelloResponse resp = iota_ServiceStub.hello(hello);
|
||
System.out.println(" Hello response: " + resp.getHelloResponse().getHello());
|
||
Identity id = new Identity();
|
||
id.setAsString(sid);
|
||
OmICron client = new OmICron(id, serviceURL);
|
||
|
||
System.out.println("Processing traceEPC ...");
|
||
IOTA_ServiceStub.TraceEPCRequest traceEPCRequest = new IOTA_ServiceStub.TraceEPCRequest();
|
||
IOTA_ServiceStub.TraceEPCRequestIn in = new IOTA_ServiceStub.TraceEPCRequestIn();
|
||
in.setEpc(epc);
|
||
traceEPCRequest.setTraceEPCRequest(in);
|
||
IOTA_ServiceStub.TraceEPCResponse respTrac = iota_ServiceStub.traceEPC(traceEPCRequest);
|
||
IOTA_ServiceStub.Event[] events = respTrac.getTraceEPCResponse().getEventList().getEvent();
|
||
if (events == null) {
|
||
List<EPCISEventType> eventList = client.traceEPC(epc);
|
||
if (eventList.isEmpty()) {
|
||
System.out.println(" No events found.");
|
||
} else {
|
||
for (IOTA_ServiceStub.Event e : events) {
|
||
for (EPCISEventType evt : eventList) {
|
||
EPCISEventTypeHelper e = new EPCISEventTypeHelper(evt);
|
||
System.out.println(" Event found: " + e.getBizStep() + " " + e.getDisposition());
|
||
}
|
||
}
|
Formats disponibles : Unified diff
Version 1.9-mock
This is version 1.9-mock. 1.9 because the API still have changes pending
(principally relative to the Discovery Services). “mock” because TLS
configuration is not yet available and the signatures (SigMa) are not fully
implemented.
- code cleaned and refactored
- lots of bugs fixed
- dependencies checked and trimmed
- documentation added
- Identity handling added
- code refactored
- new, better APIs
- Identity handling added
- use EPCglobal and DS events (no proxy types anymore)
- ETa-Callback modules are now available as web applications
- filtering rules: if a part of an event is not allowed, now the whole
event is deleted from the result (before only the rejectd part was)
- new temporary User web service
- new Xi module: XACML Interrogation web service (was two modules: TCP and
servlet)
- ETa and its Callback modules
- ActiveMQ
- SigMa
- certificate/signing key
- new simplified figures (sans IoTa and simplified IoTa)
- new figure for ETa modules
- show 3rd party clients
- data flows specified
- TLS and link security added
- IDs and trusted IDs added
- color adjusted for printing
- GREYC logo added