Skip to content

Commit

Permalink
use helper put method in plant
Browse files Browse the repository at this point in the history
  • Loading branch information
justinwb committed Jan 7, 2022
1 parent a1e8b51 commit 4365aed
Showing 1 changed file with 5 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.janeirodigital.shapetrees.core.*;
import com.janeirodigital.shapetrees.core.enums.ContentType;
import com.janeirodigital.shapetrees.core.enums.HttpHeader;
import com.janeirodigital.shapetrees.core.exceptions.ShapeTreeException;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.apache.jena.riot.Lang;
import org.apache.jena.riot.RDFDataMgr;

import java.io.IOException;
import java.io.StringWriter;
import java.net.URL;
import java.util.List;
Expand All @@ -18,7 +16,8 @@
import static com.janeirodigital.shapetrees.core.ManageableInstance.getInstance;
import static com.janeirodigital.shapetrees.core.enums.ContentType.SPARQL_UPDATE;
import static com.janeirodigital.shapetrees.core.enums.ContentType.TEXT_TURTLE;
import static com.janeirodigital.shapetrees.core.enums.HttpHeader.*;
import static com.janeirodigital.shapetrees.core.enums.HttpHeader.CONTENT_TYPE;
import static com.janeirodigital.shapetrees.core.enums.HttpHeader.SLUG;
import static com.janeirodigital.shapetrees.core.enums.LinkRelation.*;
import static com.janeirodigital.shapetrees.core.vocabularies.LdpVocabulary.BASIC_CONTAINER;
import static com.janeirodigital.shapetrees.okhttp.OkHttpHelper.*;
Expand Down Expand Up @@ -114,16 +113,9 @@ public static Response plant(OkHttpClient okHttpClient, ShapeTreeContext context
log.debug("Resource {} is not currently managed by a shape tree", resourceUrl);
}

// Initialize a shape tree assignment based on the supplied parameters
// Initialize a shape tree assignment based on the supplied parameters and add it to the manager
URL assignmentUrl = manager.mintAssignmentUrl();
ShapeTreeAssignment assignment = new ShapeTreeAssignment(shapeTreeUrl,
resourceUrl,
assignmentUrl,
focusNodeUrl,
shapeTree.getShape(),
assignmentUrl);

// Add the assignment to the manager
ShapeTreeAssignment assignment = new ShapeTreeAssignment(shapeTreeUrl, resourceUrl, assignmentUrl, focusNodeUrl, shapeTree.getShape(), assignmentUrl);
manager.addAssignment(assignment);

// Get an RDF version of the manager stored in a turtle string
Expand All @@ -132,22 +124,7 @@ public static Response plant(OkHttpClient okHttpClient, ShapeTreeContext context

log.debug("Updating shape tree manager resource at {}", manager.getId());

Request.Builder requestBuilder = new Request.Builder();
requestBuilder.url(managerResourceUrl);
RequestBody requestBody = RequestBody.create(sw.toString(), MediaType.get("text/turtle"));
requestBuilder.method("PUT", requestBody);
if (context.hasCredentials()) {
Headers headers = setHttpHeader(HttpHeader.AUTHORIZATION, context.getCredentials());
requestBuilder.headers(headers);
}

try (Response response = okHttpClient.newCall(requestBuilder.build()).execute()) {
// wrapping the call in try-with-resources automatically closes the response
return checkResponse(response);
} catch (IOException ex) {
throw new ShapeTreeException(500, "Failed to put remote resource: " + ex.getMessage());
}

return putHttpResource(okHttpClient, managerResourceUrl, context.getCredentials(), null, sw.toString(), TEXT_TURTLE);
}

public static Response unplant(OkHttpClient okHttpClient, ShapeTreeContext context, URL resourceUrl, URL shapeTreeUrl) throws ShapeTreeException {
Expand Down

0 comments on commit 4365aed

Please sign in to comment.