Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Basic authentication for Ontology Loader and import wizard #745

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@
<dependency>
<groupId>net.sourceforge.owlapi</groupId>
<artifactId>owlapi-osgidistribution</artifactId>
<version>4.2.8</version>
<version>4.2.9</version>
</dependency>

<dependency>
Expand Down Expand Up @@ -226,7 +226,13 @@
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>


<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

<dependency>
<groupId>edu.stanford.protege</groupId>
<artifactId>org.protege.xmlcatalog</artifactId>
Expand Down
1 change: 1 addition & 0 deletions protege-desktop/src/main/assembly/dependency-sets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<include>org.slf4j:log4j-over-slf4j:jar</include>
<include>org.slf4j:jul-to-slf4j:jar</include>
<include>commons-io:commons-io:jar</include>
<include>commons-codec:commons-codec:jar</include>
<include>com.google.inject:guice:jar</include>
<include>com.google.inject.extensions:guice-assistedinject:jar</include>
<include>com.google.inject.extensions:guice-multibindings:jar</include>
Expand Down
1 change: 1 addition & 0 deletions protege-desktop/src/main/assembly/protege-os-x.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
<include>org.slf4j:log4j-over-slf4j:jar</include>
<include>org.slf4j:jul-to-slf4j:jar</include>
<include>commons-io:commons-io:jar</include>
<include>commons-codec:commons-codec:jar</include>
<include>com.google.inject:guice:jar</include>
<include>com.google.inject.extensions:guice-assistedinject:jar</include>
<include>com.google.inject.extensions:guice-multibindings:jar</include>
Expand Down
6 changes: 6 additions & 0 deletions protege-editor-owl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.9</version>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.protege.editor.owl.ui.UIHelper;
import org.protege.editor.owl.ui.error.OntologyLoadErrorHandlerUI;
import org.protege.editor.owl.ui.explanation.ExplanationManager;
import org.protege.editor.owl.ui.ontology.authentication.BasicAuthenticationDialog;
import org.protege.editor.owl.ui.ontology.OntologyPreferences;
import org.protege.editor.owl.ui.ontology.imports.missing.MissingImportHandlerUI;
import org.semanticweb.owlapi.formats.PrefixDocumentFormat;
Expand Down Expand Up @@ -66,6 +67,8 @@ public class OWLEditorKit extends AbstractEditorKit<OWLEditorKitFactory> {
private final Set<URI> newPhysicalURIs = new HashSet<>();

private final OntologyLoadErrorHandlerUI loadErrorHandler;

private final BasicAuthenticationDialog basicAuthenticationDialogHandler;

private final ServiceRegistration<?> registration;

Expand Down Expand Up @@ -97,7 +100,9 @@ public OWLEditorKit(@Nonnull OWLEditorKitFactory editorKitFactory) {
modelManager.setExplanationManager(new ExplanationManager(this));
modelManager.setMissingImportHandler(new MissingImportHandlerUI(this));
loadErrorHandler = new OntologyLoadErrorHandlerUI(this);
basicAuthenticationDialogHandler = new BasicAuthenticationDialog(this);
modelManager.setLoadErrorHandler(loadErrorHandler);
modelManager.setBasicAuthenticationHandler(basicAuthenticationDialogHandler);


loadIOListenerPlugins();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.protege.editor.owl.ui.explanation.ExplanationManager;
import org.protege.editor.owl.ui.renderer.OWLModelManagerEntityRenderer;
import org.protege.editor.owl.ui.renderer.OWLObjectRenderer;
import org.protege.editor.owl.ui.ontology.authentication.BasicAuthenticationHandler;
import org.protege.xmlcatalog.XMLCatalog;
import org.semanticweb.owlapi.model.*;
import org.semanticweb.owlapi.reasoner.OWLReasoner;
Expand Down Expand Up @@ -312,6 +313,12 @@ default void setSaveErrorHandler(SaveErrorHandler handler) {


void setLoadErrorHandler(OntologyLoadErrorHandler handler);

/**
* Set the basic authentication value.
* @param handler
*/
void setBasicAuthenticationHandler(BasicAuthenticationHandler handler);

XMLCatalog addRootFolder(File dir);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.protege.editor.owl.ui.explanation.ExplanationManager;
import org.protege.editor.owl.ui.renderer.*;
import org.protege.editor.owl.ui.renderer.plugin.RendererPlugin;
import org.protege.editor.owl.ui.ontology.authentication.BasicAuthenticationHandler;
import org.protege.xmlcatalog.XMLCatalog;
import org.semanticweb.owlapi.apibinding.OWLManager;
import org.semanticweb.owlapi.formats.RDFXMLDocumentFormat;
Expand Down Expand Up @@ -130,6 +131,8 @@ public class OWLModelManagerImpl extends AbstractModelManager implements OWLMode
// error handlers

private OntologyLoadErrorHandler loadErrorHandler;

private BasicAuthenticationHandler basicAuthenticationHandler;

private final UserResolvedIRIMapper userResolvedIRIMapper = new UserResolvedIRIMapper(new MissingImportHandlerImpl());

Expand Down Expand Up @@ -273,11 +276,63 @@ public boolean loadOntologyFromPhysicalURI(URI uri) {
return loadedOntology.isPresent();
} catch (OWLOntologyCreationException e) {
OWLOntologyID id = new OWLOntologyID(com.google.common.base.Optional.of(IRI.create(uri)), com.google.common.base.Optional.<IRI>absent());
handleLoadError(id, uri, e);
// Check error message contain HTTP response code: 401 or 403 status then show the popup for basic authentication
if (e.getMessage().contains("Server returned HTTP response code: 401") || e.getMessage().contains("Server returned HTTP response code: 403")) {
logger.info(e.getMessage());
String authenticationValue = handleBasicAuthentication(id, uri, e);
if (authenticationValue != null) {
return loadOntologyFromPhysicalURIWithAuthentication(uri, authenticationValue);
} else {
handleLoadError(id, uri, e);
}
} else {
handleLoadError(id, uri, e);
}
return false;
}
}

/**
* A convenience method that loads an ontology from a file The location of
* the file is specified by the URI argument with basic authentication value.
*/
public boolean loadOntologyFromPhysicalURIWithAuthentication(URI uri, String authenticationValue) {
Stopwatch stopwatch = Stopwatch.createUnstarted();
try {
logger.info(LogBanner.start("Loading Ontology"));
logger.info("Loading ontology from {}", uri);
stopwatch.start();
if (UIUtil.isLocalFile(uri)) {
// Load the URIs of other ontologies that are contained in the same folder.
File parentFile = new File(uri).getParentFile();
addRootFolder(parentFile);
}
OntologyLoader loader = new OntologyLoader(this, userResolvedIRIMapper,authenticationValue);
Optional<OWLOntology> loadedOntology = loader.loadOntology(uri);
logger.info("Loading for ontology and imports closure successfully completed in {} ms", stopwatch.elapsed(TimeUnit.MILLISECONDS));
loadedOntology.ifPresent(ontology -> {
DocumentFormatUpdater formatUpdater = new DocumentFormatUpdater(new DocumentFormatMapper());
formatUpdater.updateFormat(ontology);
});
logger.info(LogBanner.end());
return loadedOntology.isPresent();
} catch (OWLOntologyCreationException e) {
OWLOntologyID id = new OWLOntologyID(com.google.common.base.Optional.of(IRI.create(uri)), com.google.common.base.Optional.<IRI>absent());
// Check error message contain HTTP response code: 401 or 403 status then show the popup for basic authentication
if (e.getMessage().contains("Server returned HTTP response code: 401") || e.getMessage().contains("Server returned HTTP response code: 403")) {
logger.info(e.getMessage());
String base64AuthenticationValue = handleBasicAuthentication(id, uri, e);
if (base64AuthenticationValue != null) {
return loadOntologyFromPhysicalURIWithAuthentication(uri, base64AuthenticationValue);
} else {
handleLoadError(id, uri, e);
}
} else {
handleLoadError(id, uri, e);
}
return false;
}
}


public void startedLoadingOntology(@Nonnull LoadingStartedEvent event) {
logger.info("Loading {} from {}", event.getOntologyID(), event.getDocumentIRI());
Expand All @@ -302,6 +357,18 @@ private void handleLoadError(OWLOntologyID owlOntologyID, URI documentURI, Excep
}
}
}

private String handleBasicAuthentication(OWLOntologyID owlOntologyID, URI documentURI, Exception e) {
if (basicAuthenticationHandler != null) {
try {
return basicAuthenticationHandler.handleBasicAuthenticationOntology(owlOntologyID, documentURI, e);
} catch (Throwable e1) {
// if, for any reason, the loadErrorHandler cannot report the error
ErrorLogPanel.showErrorDialog(e1);
}
}
return null;
}

public XMLCatalog addRootFolder(File dir) {
return ontologyCatalogManager.addFolder(dir);
Expand Down Expand Up @@ -958,6 +1025,16 @@ public void setMissingImportHandler(MissingImportHandler missingImportHandler) {
public void setLoadErrorHandler(OntologyLoadErrorHandler handler) {
this.loadErrorHandler = handler;
}

//////////////////////////////////////////////////////////////////////////////////////
//
// Basic Authentication handling
//
//////////////////////////////////////////////////////////////////////////////////////


public void setBasicAuthenticationHandler(BasicAuthenticationHandler handler) {
this.basicAuthenticationHandler = handler;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public class OntologyLoader {
private final OWLModelManager modelManager;

private final UserResolvedIRIMapper userResolvedIRIMapper;

private String authenticationValue = null;

private final ProgressDialog dlg = new ProgressDialog();

Expand All @@ -46,6 +48,12 @@ public OntologyLoader(OWLModelManager modelManager, UserResolvedIRIMapper userRe
this.modelManager = modelManager;
this.userResolvedIRIMapper = userResolvedIRIMapper;
}

public OntologyLoader(OWLModelManager modelManager, UserResolvedIRIMapper userResolvedIRIMapper,String authenticationValue) {
this.modelManager = modelManager;
this.userResolvedIRIMapper = userResolvedIRIMapper;
this.authenticationValue = authenticationValue;
}

public Optional<OWLOntology> loadOntology(URI documentUri) throws OWLOntologyCreationException {
if (!SwingUtilities.isEventDispatchThread()) {
Expand Down Expand Up @@ -99,6 +107,9 @@ private Optional<OWLOntology> loadOntologyInternal(URI documentURI) throws OWLOn
loadingManager.addOntologyLoaderListener(new ProgressDialogOntologyLoaderListener(dlg, logger));
OWLOntologyLoaderConfiguration configuration = new OWLOntologyLoaderConfiguration();
configuration = configuration.setMissingImportHandlingStrategy(MissingImportHandlingStrategy.SILENT);
if(authenticationValue != null && !authenticationValue.isEmpty()) {
configuration = configuration.setAuthorizationValue(authenticationValue);
}
IRIDocumentSource documentSource = new IRIDocumentSource(IRI.create(documentURI));
OWLOntology ontology = loadingManager.loadOntologyFromOntologyDocument(documentSource, configuration);
Set<OWLOntology> alreadyLoadedOntologies = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import org.protege.editor.owl.model.repository.extractors.LastResortExtractor;
import org.protege.editor.owl.model.repository.extractors.OntologyIdExtractor;
import org.protege.editor.owl.model.repository.extractors.RdfXmlExtractor;
import org.protege.editor.owl.ui.error.OntologyLoadErrorHandlerUI;
import org.protege.editor.owl.ui.ontology.authentication.BasicAuthenticationDialog;
import org.semanticweb.owlapi.model.OWLOntologyID;

import java.net.URI;
Expand All @@ -15,10 +17,15 @@ public class MasterOntologyIDExtractor implements OntologyIdExtractor {
private List<OntologyIdExtractor> extractors = new ArrayList<>();

// TODO - all the callers of this method have problems which need fixing.
public MasterOntologyIDExtractor() {
public MasterOntologyIDExtractor() {
extractors.add(new RdfXmlExtractor());
extractors.add(new LastResortExtractor());
}

public MasterOntologyIDExtractor(BasicAuthenticationDialog basicAuthenticationDialogHandler, OntologyLoadErrorHandlerUI ontologyLoadErrorHandler) {
extractors.add(new RdfXmlExtractor());
extractors.add(new LastResortExtractor(basicAuthenticationDialogHandler, ontologyLoadErrorHandler));
}

public Optional<OWLOntologyID> getOntologyId(URI location) {
Optional<OWLOntologyID> id = Optional.absent();
Expand Down
Loading