Skip to content

Commit

Permalink
Update Geogig plugin to incorporate web api conversion to spring.
Browse files Browse the repository at this point in the history
Signed-off-by: Johnathan Garrett <jd@prominentedge.com>
  • Loading branch information
jdgarrett authored and Erik Merkle committed Sep 22, 2017
1 parent 3c5093c commit 722d240
Show file tree
Hide file tree
Showing 31 changed files with 925 additions and 970 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import java.io.IOException;
import java.net.URI;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
package org.geogig.geoserver.rest;

import static org.locationtech.geogig.web.api.RESTUtils.getStringAttribute;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -21,12 +19,10 @@
import org.locationtech.geogig.repository.RepositoryConnectionException;
import org.locationtech.geogig.repository.RepositoryResolver;
import org.locationtech.geogig.repository.impl.GeoGIG;
import org.locationtech.geogig.rest.RestletException;
import org.locationtech.geogig.rest.repository.InitRequestUtil;
import org.locationtech.geogig.rest.repository.RepositoryProvider;
import org.restlet.data.Method;
import org.restlet.data.Request;
import org.restlet.data.Status;
import org.locationtech.geogig.web.api.CommandSpecException;
import org.springframework.http.HttpStatus;

import com.google.common.base.Function;
import com.google.common.base.Optional;
Expand All @@ -36,7 +32,7 @@

/**
* {@link RepositoryProvider} that looks up the coresponding {@link GeoGIG} instance to a given
* {@link Request} by asking the geoserver's {@link RepositoryManager}
* {@link HttpServletRequest} by asking the geoserver's {@link RepositoryManager}
*/
public class GeoServerRepositoryProvider implements RepositoryProvider {

Expand All @@ -50,32 +46,6 @@ public class GeoServerRepositoryProvider implements RepositoryProvider {
*/
public static final String IMPORT_CMD = "importExistingRepo";

private Optional<String> getRepositoryName(Request request) {
final String repo = getStringAttribute(request, "repository");
return Optional.fromNullable(repo);
}

public Optional<RepositoryInfo> findRepository(Request request) {
Optional<String> repositoryName = getRepositoryName(request);
if (!repositoryName.isPresent()) {
return Optional.absent();
}
try {
String repoName = repositoryName.get();
String repoId = getRepoIdForName(repoName);
if (repoId != null) {
RepositoryManager repositoryManager = RepositoryManager.get();
RepositoryInfo repositoryInfo;
repositoryInfo = repositoryManager.get(repoId);
return Optional.of(repositoryInfo);
} else {
return Optional.absent();
}
} catch (RuntimeException e) {
return Optional.absent();
}
}

public List<RepositoryInfo> getRepositoryInfos() {
return RepositoryManager.get().getAll();
}
Expand All @@ -90,14 +60,13 @@ private String getRepoIdForName(String repoName) {
}
return null;
}

@Override
public void delete(Request request) {
Optional<Repository> geogig = getGeogig(request);
public void delete(String repoName) {
Optional<Repository> geogig = getGeogig(repoName);
Preconditions.checkState(geogig.isPresent(), "No repository to delete.");

final String repositoryName = getStringAttribute(request, "repository");
final String repoId = getRepoIdForName(repositoryName);
final String repoId = getRepoIdForName(repoName);
Repository ggig = geogig.get();
Optional<URI> repoUri = ggig.command(ResolveGeogigURI.class).call();
Preconditions.checkState(repoUri.isPresent(), "No repository to delete.");
Expand All @@ -110,7 +79,6 @@ public void delete(Request request) {
} catch (Exception e) {
Throwables.propagate(e);
}

}

@Override
Expand All @@ -132,59 +100,32 @@ public String apply(RepositoryInfo input) {
}
});
}

private boolean isInitRequest(Request request) {
// if the request is a PUT, and the request path ends in "init", it's an INIT request.
if (Method.PUT.equals(request.getMethod())) {
Map<String, Object> attributes = request.getAttributes();
if (attributes != null && attributes.containsKey("command")) {
return INIT_CMD.equals(attributes.get("command"));
} else if (request.getResourceRef() != null) {
String path = request.getResourceRef().getPath();
return path != null && path.contains(INIT_CMD);
}

@Override
public Repository createGeogig(String repositoryName, Map<String, String> parameters) {
if (repositoryName != null && RepositoryManager.get().repoExistsByName(repositoryName)) {
// repo already exists
throw new CommandSpecException(
"The specified repository name is already in use, please try a different name",
HttpStatus.CONFLICT);
}
return false;
}

private boolean isImportRequest(Request request) {
// if the request is a POST, and the request path ends in "importExistingRepo"
if (Method.POST.equals(request.getMethod())) {
Map<String, Object> attributes = request.getAttributes();
if (attributes != null && attributes.containsKey("command")) {
return IMPORT_CMD.equals(attributes.get("command"));
} else if (request.getResourceRef() != null) {
String path = request.getResourceRef().getPath();
return path != null && path.contains(IMPORT_CMD);
}
Optional<Repository> initRepo = AddRepoRequestHandler.createGeoGIG(repositoryName, parameters);
if (initRepo.isPresent()) {
// init request was sufficient
return initRepo.get();
}
return false;
}
return null;

@Override
public Optional<Repository> getGeogig(Request request) {
Optional<String> repositoryName = getRepositoryName(request);
if (!repositoryName.isPresent()) {
return Optional.absent();
}
// look for one with the provided name first
Optional<Repository> geogig = getGeogig(repositoryName.get());
if (!geogig.isPresent()) {
if (isInitRequest(request)) {
// special handling of INIT requests
geogig = AddRepoRequestHandler.createGeoGIG(request);
} else if (isImportRequest(request)){
// handles IMPORT requests
geogig = AddRepoRequestHandler.importGeogig(request);
}
}
if (!geogig.isPresent()) {
// if it's still not present, just generate one.
// This is so the CommandResource can get into the runCmd code before failing and
// generating the correct responses
geogig = Optional.fromNullable(RepositoryManager.get().createRepo(new Hints()));
}

public Repository importExistingGeogig(String repositoryName, Map<String, String> parameters) {
Optional<Repository> importRepo = AddRepoRequestHandler.importGeogig(repositoryName, parameters);
if (importRepo.isPresent()) {
// import request was sufficient
return importRepo.get();
}
return geogig;
return null;
}

public Optional<Repository> getGeogig(String repositoryName) {
Expand All @@ -202,38 +143,41 @@ private Repository findRepository(String repositoryName) {
try {
return manager.getRepository(repoId);
} catch (IOException e) {
throw new RestletException("Error accessing datastore " + repositoryName,
Status.SERVER_ERROR_INTERNAL, e);
throw new CommandSpecException("Error accessing datastore " + repositoryName,
HttpStatus.INTERNAL_SERVER_ERROR, e);
}
}


private static class AddRepoRequestHandler {

private static Optional<Repository> createGeoGIG(Request request) {
private static Optional<Repository> createGeoGIG(String repositoryName, Map<String, String> parameters) {
try {
final Hints hints = InitRequestUtil.createHintsFromRequest(request);
// now build the repo with the Hints
return Optional.fromNullable(RepositoryManager.get().createRepo(hints));
final Hints hints = InitRequestUtil.createHintsFromParameters(repositoryName,
parameters);
final Repository repository = RepositoryManager.get().createRepo(hints);
return Optional.fromNullable(repository);
} catch (Exception ex) {
Throwables.propagate(ex);
}
return Optional.absent();
}

private static Optional<Repository> importGeogig(Request request) {

private static Optional<Repository> importGeogig(String repositoryName, Map<String, String> parameters) {
// if the request is a POST, and the request path ends in "importExistingRepo"

try {
// build URI
Hints hint = InitRequestUtil.createHintsFromRequest(request);
final Hints hints = InitRequestUtil.createHintsFromParameters(repositoryName,
parameters);

// now build the repo with the Hints
RepositoryInfo repoInfo = new RepositoryInfo();

// set the repo location from the URI
if (!hint.get(Hints.REPOSITORY_URL).isPresent()) {
return Optional.absent();
if (!hints.get(Hints.REPOSITORY_URL).isPresent()) {
return Optional.absent();
}
URI uri = new URI(hint.get(Hints.REPOSITORY_URL).get().toString());
URI uri = new URI(hints.get(Hints.REPOSITORY_URL).get().toString());
repoInfo.setLocation(uri);

// check to see if repo is initialized
Expand All @@ -254,4 +198,18 @@ private static Optional<Repository> importGeogig(Request request) {
return Optional.absent();
}
}

@Override
public boolean hasGeoGig(String repositoryName) {
if (null != repositoryName) {
Iterator<String> findRepositories = findRepositories();
while (findRepositories.hasNext()) {
String next = findRepositories.next();
if (next.equals(repositoryName)) {
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* (c) 2017 Open Source Geospatial Foundation - all rights reserved
* This code is licensed under the GPL 2.0 license, available at the root
* application directory.
*/
package org.geogig.geoserver.rest;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.AnnotationBeanNameGenerator;

/**
* Bean name generator that prefixes geogig in front of the default bean name to avoid naming conflicts between geogig and geoserver beans.
*/
public class GeogigBeanNameGenerator extends AnnotationBeanNameGenerator {

public GeogigBeanNameGenerator() {
super();
}

@Override
public String generateBeanName(BeanDefinition arg0, BeanDefinitionRegistry arg1) {
return "geogig" + super.generateBeanName(arg0, arg1);
}

}

0 comments on commit 722d240

Please sign in to comment.