📄 soapservices.java
字号:
/* OMM - Ontology Middleware Module * Copyright (C) 2002 OntoText Lab, Sirma AI OOD * * Contact: * Sirma AI OOD, OntoText Lab. * 38A, Christo Botev Blvd. * 1000 Sofia, Bulgaria * tel. +359(2)981 00 18 * fax. +359(2)981 90 58 * info@ontotext.com * * http://www.ontotext.com/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.server.soap;/** * SOAP Services <br> * This class provides SOAP Access to OMM/Sesame. * <br> Provides access to the RdfSchemaSource, RequestRouter * and VersionManagement interfaces. * @author damyan ognyanov * @author borislav popov */import java.io.IOException;import java.util.Iterator;import java.util.Map;import java.util.Vector;import org.openrdf.model.BNode;import org.openrdf.model.Literal;import org.openrdf.model.Resource;import org.openrdf.model.Statement;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.impl.BNodeImpl;import org.openrdf.model.impl.LiteralImpl;import org.openrdf.model.impl.URIImpl;import org.openrdf.rio.rdfxml.RdfXmlWriter;import org.openrdf.sesame.admin.AdminListener;import org.openrdf.sesame.admin.XmlAdminMsgWriter;import org.openrdf.sesame.config.AccessDeniedException;import org.openrdf.sesame.config.ConfigurationException;import org.openrdf.sesame.config.RepositoryConfig;import org.openrdf.sesame.config.UnknownRepositoryException;import org.openrdf.sesame.config.UserInfo;import org.openrdf.sesame.constants.QueryLanguage;import org.openrdf.sesame.constants.RDFFormat;import org.openrdf.sesame.omm.SessionContext;import org.openrdf.sesame.omm.SessionKilled;import org.openrdf.sesame.omm.VersionManagement;import org.openrdf.sesame.query.QueryErrorType;import org.openrdf.sesame.query.TableQueryResultListener;import org.openrdf.sesame.repository.local.LocalRepository;import org.openrdf.sesame.repository.local.LocalService;import org.openrdf.sesame.sail.RdfSchemaSource;import org.openrdf.sesame.sail.Sail;import org.openrdf.sesame.sail.StatementIterator;import org.openrdf.sesame.server.SesameServer;public class SoapServices { private final static String VERSION_SAIL = "org.openrdf.sesame.omm.VersionManagement"; static int lastSessionID = 0; private VersionManagement verSail = null; private LocalRepository _repository; static int createSession() { return ++lastSessionID; } int theID; SessionKilled sk = null; public SoapServices() { theID = createSession(); } private SessionContext getContext() { String thisSessionID = "SOAP["+theID+"]"; SessionContext sc = SessionContext.get(thisSessionID); if (sc == null) { sc = new SessionContext(); SessionContext.put(thisSessionID, sc); System.out.println("new context created ("+thisSessionID+")"); UserInfo ui = SesameServer.getSystemConfig().getUserInfo(2); // anonymous if (ui != null) { sc.userID = ui.getID(); sc.user = ui.getLogin(); sc.pass = ui.getPassword(); } sk = new SessionKilled(thisSessionID); } SessionContext.setContext(sc); return sc; } private VersionManagement getVersionManagement() { VersionManagement verSail = null; // get the versioning sail if such try { SessionContext sc = getContext(); LocalService service = SesameServer.getLocalService(); service.login(sc.user, sc.pass); LocalRepository rep = (LocalRepository)service.getRepository(sc.repository); Sail sail = rep.getSail(); verSail = (VersionManagement)org.openrdf.sesame.sail.util.QuerySailStack.queryInterface( sail, VERSION_SAIL); } catch (Exception xx) { throw new RuntimeException(xx.getMessage()); } return verSail; } public int uploadData(String data, String baseURL) { SessionContext sc = getContext(); java.io.OutputStream os = new java.io.ByteArrayOutputStream(); try { AdminListener report = new XmlAdminMsgWriter(os); // FIXME the assumption is made that the format is XML _repository.addData(data, baseURL, RDFFormat.RDFXML, true, report); } catch (AccessDeniedException e) { throw new RuntimeException(e.getMessage()); } catch (java.io.IOException ex) { throw new RuntimeException(ex.getMessage()); } return 0; } public int addDataFromUrl( String dataUrl, String baseURL) { java.net.URL url = null; try { // Check existence of URL url = new java.net.URL(dataUrl); } catch (java.net.MalformedURLException e) { throw new RuntimeException(e.getMessage()); } SessionContext sc = getContext(); java.io.OutputStream os = new java.io.ByteArrayOutputStream(); try { AdminListener report = new XmlAdminMsgWriter(os); // FIXME the assumption is made that the format is XML _repository.addData(url, baseURL, RDFFormat.RDFXML, true, report); } catch (java.io.IOException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return 0; } public boolean clearRepository() { SessionContext sc = getContext(); java.io.OutputStream os = new java.io.ByteArrayOutputStream(); try { org.openrdf.sesame.admin.AdminListener report = new org.openrdf.sesame.admin.XmlAdminMsgWriter(os); _repository.clear(report); } catch (java.io.IOException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return true; } public int removeStatements(String subjURI, String predURI, String objURI, boolean bObjectIsLiteral) { SessionContext sc = getContext(); // check input parameters. if (subjURI != null && subjURI.trim().equals("")) { subjURI = null; } if (predURI != null && predURI.trim().equals("")) { predURI = null; } if (objURI != null && objURI.trim().equals("")) { objURI = null; } java.io.OutputStream os = new java.io.ByteArrayOutputStream(); try { org.openrdf.sesame.admin.AdminListener report = new org.openrdf.sesame.admin.XmlAdminMsgWriter(os); Resource subj = null; try { subj = new URIImpl(subjURI); } catch (IllegalArgumentException e) { subj = new BNodeImpl(subjURI); } URI pred = new URIImpl(predURI); Value obj = null; if (bObjectIsLiteral) { obj = new LiteralImpl(objURI); } else { try { obj = new URIImpl(objURI); } catch (IllegalArgumentException e) { obj = new BNodeImpl(objURI); } } _repository.removeStatements(subj, pred, obj, report); } catch (java.io.IOException e) { throw new RuntimeException(e.getMessage()); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } return 0; } public Vector evalRqlQuery( String query) throws Exception { SessionContext sc = getContext(); SoapQueryResultListener qrl = new SoapQueryResultListener(); _repository.performTableQuery(QueryLanguage.RQL, query, qrl); return qrl.result(); } public java.util.Vector evalRdqlQuery(String query) throws Exception { SessionContext sc = getContext(); SoapQueryResultListener qrl = new SoapQueryResultListener(); _repository.performTableQuery(QueryLanguage.RDQL, query, qrl); return qrl.result(); } public String extractRDF(boolean ontology, boolean instances, boolean explicit) { SessionContext sc = getContext(); java.io.StringWriter out = new java.io.StringWriter(); RdfXmlWriter documentWriter = new RdfXmlWriter(out); try { _repository.extractRDF(documentWriter, ontology, instances, explicit, true); } catch (Exception e) { e.printStackTrace(); } return out.toString(); } public String[] listRepositories() { SessionContext sc = getContext(); /* FIXME request router no longer exists. use a LocalService. try { return RequestRouter.getRepositoryIDs(); } catch (AccessDeniedException ex) { throw new RuntimeException("AccessDeniedException:"+ex.getMessage()); } catch (UnknownRepositoryException ex) { throw new RuntimeException("UnknownRepositoryException"+ex.getMessage()); } */ return new String[0]; } public boolean selectRepository(String repos) { SessionContext sc = getContext(); try { _repository = (LocalRepository)SesameServer.getLocalService().getRepository(repos); } catch (UnknownRepositoryException ex) { throw new RuntimeException("UnknownRepositoryException"+ex.getMessage()); } catch (ConfigurationException ex) { throw new RuntimeException("ConfigurationException",ex); } return true; } public String getRepositoryTitle(String repositoryId) { RepositoryConfig config = SesameServer.getSystemConfig().getRepositoryConfig(repositoryId); if ( config == null ) { return ""; } return config.getTitle(); } public boolean login(String userID, String pass) { SessionContext sc = getContext(); try { SesameServer.getLocalService().login(userID, pass); return true; } catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } } // login/** * RdfSchemaSource dellegation */ public Vector getStatements(String subj, String pred, String obj, boolean objIsLiteral) { Vector result = null; SessionContext sc = getContext(); try { LocalService service = SesameServer.getLocalService(); service.login(sc.user, sc.pass); LocalRepository rep = (LocalRepository)service.getRepository(sc.repository); Sail sail = rep.getSail(); if (sail instanceof RdfSchemaSource) { RdfSchemaSource rdfSchema = (RdfSchemaSource)sail; Resource subjR = null; if (subj != null) { subjR = new URIImpl(subj); } URI predR = null; if (pred != null) { predR = new URIImpl(pred); } Value objV = null; if (obj != null) { if (objIsLiteral) { objV = new LiteralImpl(obj); } else { objV = new URIImpl(obj); } } StatementIterator iter = rdfSchema.getStatements(subjR, predR, objV); result = _statementIteratorToVector(iter); iter.close(); } } catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } return result; } //getStatements() public Vector getExplicitStatements(String subj, String pred, String obj, boolean objIsLiteral) { Vector result = null; SessionContext sc = getContext(); try { LocalService service = SesameServer.getLocalService(); service.login(sc.user, sc.pass); LocalRepository rep = (LocalRepository)service.getRepository(sc.repository); Sail sail = rep.getSail(); if (sail instanceof RdfSchemaSource) { RdfSchemaSource rdfSchema = (RdfSchemaSource)sail; Resource subjR = null; if (subj != null) { subjR = new URIImpl(subj); } URI predR = null; if (pred != null) { predR = new URIImpl(pred); } Value objV = null; if (obj != null) { if (objIsLiteral) { objV = new LiteralImpl(obj); } else { objV = new URIImpl(obj); } } StatementIterator iter = rdfSchema.getExplicitStatements(subjR, predR, objV); result = _statementIteratorToVector(iter); iter.close(); } } catch (Exception ex) { throw new RuntimeException(ex.getMessage()); } return result; } //getExplicitStatements() protected Vector _statementIteratorToVector(StatementIterator iter) { Vector result = new Vector(); while (iter.hasNext()) { Statement st = iter.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -