|
| 1 | +/** |
| 2 | + * Copyright 2025 Martynas Jusevičius <martynas@atomgraph.com> |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + */ |
| 17 | +package com.atomgraph.linkeddatahub.client; |
| 18 | + |
| 19 | +import com.atomgraph.core.MediaTypes; |
| 20 | +import com.atomgraph.linkeddatahub.client.util.RetryingInvocationBuilder; |
| 21 | +import jakarta.ws.rs.NotFoundException; |
| 22 | +import jakarta.ws.rs.client.Entity; |
| 23 | +import jakarta.ws.rs.client.WebTarget; |
| 24 | +import jakarta.ws.rs.core.MultivaluedHashMap; |
| 25 | +import jakarta.ws.rs.core.MultivaluedMap; |
| 26 | +import jakarta.ws.rs.core.Response; |
| 27 | +import jakarta.ws.rs.core.Response.Status; |
| 28 | +import org.apache.jena.rdf.model.Model; |
| 29 | +import org.slf4j.Logger; |
| 30 | +import org.slf4j.LoggerFactory; |
| 31 | + |
| 32 | +/** |
| 33 | + * |
| 34 | + * @author Martynas.Jusevicius |
| 35 | + */ |
| 36 | +public class GraphStoreClient extends com.atomgraph.core.client.GraphStoreClient { |
| 37 | + |
| 38 | + private static final Logger log = LoggerFactory.getLogger(GraphStoreClient.class); |
| 39 | + |
| 40 | + private final long defaultDelayMillis = 5000L; |
| 41 | + private final int maxRetryCount = 3; |
| 42 | + |
| 43 | + protected GraphStoreClient(MediaTypes mediaTypes, WebTarget endpoint) { |
| 44 | + super(mediaTypes, endpoint); |
| 45 | + } |
| 46 | + |
| 47 | + protected GraphStoreClient(WebTarget endpoint) { |
| 48 | + this(new MediaTypes(), endpoint); |
| 49 | + } |
| 50 | + |
| 51 | + public static GraphStoreClient create(MediaTypes mediaTypes, WebTarget endpoint) { |
| 52 | + return new GraphStoreClient(mediaTypes, endpoint); |
| 53 | + } |
| 54 | + |
| 55 | + public static GraphStoreClient create(WebTarget endpoint) { |
| 56 | + return new GraphStoreClient(endpoint); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public boolean containsModel(String uri) { |
| 61 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 62 | + params.putSingle(GRAPH_PARAM_NAME, uri); |
| 63 | + |
| 64 | + try (Response cr = new RetryingInvocationBuilder( |
| 65 | + applyParams(params).request(getReadableMediaTypes(Model.class)), |
| 66 | + defaultDelayMillis, |
| 67 | + maxRetryCount |
| 68 | + ).head()) { |
| 69 | + return cr.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public Model getModel() { |
| 75 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 76 | + params.putSingle(DEFAULT_PARAM_NAME, Boolean.TRUE.toString()); |
| 77 | + |
| 78 | + try (Response cr = new RetryingInvocationBuilder( |
| 79 | + applyParams(params).request(getReadableMediaTypes(Model.class)), |
| 80 | + defaultDelayMillis, |
| 81 | + maxRetryCount |
| 82 | + ).get()) { |
| 83 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 84 | + throw new NotFoundException(); |
| 85 | + } |
| 86 | + return cr.readEntity(Model.class); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public Model getModel(String uri) { |
| 92 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 93 | + params.putSingle(GRAPH_PARAM_NAME, uri); |
| 94 | + |
| 95 | + try (Response cr = new RetryingInvocationBuilder( |
| 96 | + applyParams(params).request(getReadableMediaTypes(Model.class)), |
| 97 | + defaultDelayMillis, |
| 98 | + maxRetryCount |
| 99 | + ).get()) { |
| 100 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 101 | + throw new NotFoundException(); |
| 102 | + } |
| 103 | + return cr.readEntity(Model.class); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + @Override |
| 108 | + public void add(Model model) { |
| 109 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 110 | + params.putSingle(DEFAULT_PARAM_NAME, Boolean.TRUE.toString()); |
| 111 | + |
| 112 | + try (Response cr = new RetryingInvocationBuilder( |
| 113 | + applyParams(params).request(), |
| 114 | + defaultDelayMillis, |
| 115 | + maxRetryCount |
| 116 | + ).post(Entity.entity(model, getDefaultMediaType()))) { |
| 117 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 118 | + throw new NotFoundException(); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + public void add(String uri, Model model) { |
| 125 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 126 | + params.putSingle(GRAPH_PARAM_NAME, uri); |
| 127 | + |
| 128 | + try (Response cr = new RetryingInvocationBuilder( |
| 129 | + applyParams(params).request(), |
| 130 | + defaultDelayMillis, |
| 131 | + maxRetryCount |
| 132 | + ).post(Entity.entity(model, getDefaultMediaType()))) { |
| 133 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 134 | + throw new NotFoundException(); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public void putModel(Model model) { |
| 141 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 142 | + params.putSingle(DEFAULT_PARAM_NAME, Boolean.TRUE.toString()); |
| 143 | + |
| 144 | + try (Response cr = new RetryingInvocationBuilder( |
| 145 | + applyParams(params).request(), |
| 146 | + defaultDelayMillis, |
| 147 | + maxRetryCount |
| 148 | + ).put(Entity.entity(model, getDefaultMediaType()))) { |
| 149 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 150 | + throw new NotFoundException(); |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void putModel(String uri, Model model) { |
| 157 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 158 | + params.putSingle(GRAPH_PARAM_NAME, uri); |
| 159 | + |
| 160 | + try (Response cr = new RetryingInvocationBuilder( |
| 161 | + applyParams(params).request(), |
| 162 | + defaultDelayMillis, |
| 163 | + maxRetryCount |
| 164 | + ).put(Entity.entity(model, getDefaultMediaType()))) { |
| 165 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 166 | + throw new NotFoundException(); |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void deleteDefault() { |
| 173 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 174 | + params.putSingle(DEFAULT_PARAM_NAME, Boolean.TRUE.toString()); |
| 175 | + |
| 176 | + try (Response cr = new RetryingInvocationBuilder( |
| 177 | + applyParams(params).request(), |
| 178 | + defaultDelayMillis, |
| 179 | + maxRetryCount |
| 180 | + ).delete()) { |
| 181 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 182 | + throw new NotFoundException(); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + @Override |
| 188 | + public void deleteModel(String uri) { |
| 189 | + MultivaluedMap<String, String> params = new MultivaluedHashMap<>(); |
| 190 | + params.putSingle(GRAPH_PARAM_NAME, uri); |
| 191 | + |
| 192 | + try (Response cr = new RetryingInvocationBuilder( |
| 193 | + applyParams(params).request(), |
| 194 | + defaultDelayMillis, |
| 195 | + maxRetryCount |
| 196 | + ).delete()) { |
| 197 | + if (cr.getStatus() == Status.NOT_FOUND.getStatusCode()) { |
| 198 | + throw new NotFoundException(); |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + |
| 203 | +} |
0 commit comments