+ /**
+ * Starts inserting the given project.
+ *
+ * @param project
+ * The project to insert
+ * @throws JSiteException
+ * if the project’s node is not connected, or no node is
+ * connected at all
+ */
+ public void insertProject(Project project) throws JSiteException {
+ Request request = new Request();
+ request.setClientToken(generateClientToken(project));
+ Node wantedNode = project.getNode();
+ if (wantedNode == null) {
+ for (Node node : nodeManager.getNodes()) {
+ if (nodeManager.getFcpClient(node) != null) {
+ wantedNode = node;
+ break;
+ }
+ }
+ }
+ if (wantedNode == null) {
+ /* TODO use custom exception */
+ throw new JSiteException("No node connected.");
+ }
+ FcpClient fcpClient = nodeManager.getFcpClient(wantedNode);
+ }
+
+ //
+ // PRIVATE METHODS
+ //
+
+ /**
+ * Checks whether the given client token is a client token created by this
+ * request manager.
+ *
+ * @param clientToken
+ * The client token to check
+ * @return {@code true} if the client token was created by this request
+ * manager, {@code false} otherwise
+ */
+ private boolean isKnownClientToken(String clientToken) {
+ String[] clientTokenParts = clientToken.split("\\.");
+ if (clientTokenParts.length != 3) {
+ return false;
+ }
+ String projectIdString = clientTokenParts[0];
+ if (projectIdString.length() != (IdGenerator.DEFAULT_LENGTH * 2)) {
+ return false;
+ }
+ int clientTokenHashCode = -1;
+ try {
+ Hex.toByte(projectIdString);
+ Long.valueOf(clientTokenParts[1]);
+ clientTokenHashCode = Integer.valueOf(clientTokenParts[2]);
+ } catch (NumberFormatException nfe1) {
+ return false;
+ }
+ if ((clientTokenParts[0] + "." + clientTokenParts[1]).hashCode() != clientTokenHashCode) {
+ return false;
+ }
+ return true;
+ }
+
+ /**
+ * Generates a client token for the given project.
+ *
+ * @param project
+ * The project to generate a client token for
+ * @return The generated client token
+ */
+ public String generateClientToken(Project project) {
+ String clientToken = project.getId() + "." + System.currentTimeMillis();
+ clientToken += "." + clientToken.hashCode();
+ return clientToken;
+ }
+
+ /**
+ * Wraps the requests of the FCP API into jSite requests.
+ *
+ * @param requests
+ * The requests to wrap
+ * @return The wrapped requests
+ */
+ private Collection<Request> wrapRequests(Collection<net.pterodactylus.fcp.highlevel.Request> requests) {
+ Collection<Request> wrappedRequests = new HashSet<Request>();
+ for (net.pterodactylus.fcp.highlevel.Request request : requests) {
+ Request wrappedRequest = new Request(request.getIdentifier());
+ wrappedRequest.setClientToken(request.getClientToken());
+ wrappedRequests.add(wrappedRequest);
+ }
+ return wrappedRequests;
+ }
+