org.glassfish.admin.rest.client.RestClientBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-client Show documentation
Show all versions of rest-client Show documentation
Rest Interface for GlassFish Management and Monitoring
The newest version!
/*
* Copyright (c) 2011, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/
package org.glassfish.admin.rest.client;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedHashMap;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response;
import org.codehaus.jettison.json.JSONObject;
import org.glassfish.admin.rest.client.utils.Util;
/**
*
* @author jasonlee
*/
public abstract class RestClientBase {
protected static final String RESPONSE_TYPE = MediaType.APPLICATION_JSON;
protected Map entityValues = new HashMap();
protected List children;
protected int status;
protected String message;
protected Client client;
protected RestClientBase parent;
private boolean initialized = false;
private boolean isNew = false;
protected RestClientBase(Client c, RestClientBase p) {
client = c;
parent = p;
}
protected RestClientBase getParent() {
return parent;
}
protected String getRestUrl() {
return getParent().getRestUrl() + getSegment();
}
protected abstract String getSegment();
public int getStatus() {
return status;
}
public String getMessage() {
return message;
}
public boolean save() {
Response response = client.target(getRestUrl()).request(RESPONSE_TYPE)
.post(buildMultivalueMapEntity(entityValues, MediaType.MULTIPART_FORM_DATA_TYPE), Response.class);
boolean success = isSuccess(response);
if (!success) {
status = response.getStatus();
message = response.readEntity(String.class);
} else {
isNew = false;
}
return success;
}
public boolean delete() {
Response response = client.target(getRestUrl()).request(RESPONSE_TYPE).delete(Response.class);
boolean success = isSuccess(response);
if (!success) {
status = response.getStatus();
message = response.readEntity(String.class);
}
return success;
}
public RestResponse execute(Method method, String endpoint, boolean needsMultiPart) {
return execute(method, endpoint, new HashMap(), needsMultiPart);
}
public RestResponse execute(Method method, String endPoint, Map payload) {
return execute(method, endPoint, payload, false);
}
public RestResponse execute(Method method, String endPoint, Map payload, boolean needsMultiPart) {
final WebTarget target = client.target(getRestUrl() + endPoint);
Response clientResponse;
switch (method) {
case POST: {
// TODO - JERSEY2
// if (needsMultiPart) {
// clientResponse = target
// .request(RESPONSE_TYPE)
// .post(buildFormDataMultipart(payload, MediaType.MULTIPART_FORM_DATA_TYPE), Response.class);
// } else
{
clientResponse = target.request(RESPONSE_TYPE).post(buildMultivalueMapEntity(payload, null), Response.class);
}
break;
}
case PUT: {
// TODO - JERSEY2
// if (needsMultiPart) {
// clientResponse = target
// .request(RESPONSE_TYPE)
// .put(buildFormDataMultipart(payload, MediaType.MULTIPART_FORM_DATA_TYPE), Response.class);
// } else
{
clientResponse = target.request(RESPONSE_TYPE).put(buildMultivalueMapEntity(payload, null), Response.class);
}
break;
}
case DELETE: {
// addQueryParams(payload, target);
clientResponse = targetWithQueryParams(target, buildMultivalueMap(payload)).request(RESPONSE_TYPE).delete(Response.class);
break;
}
default: {
// addQueryParams(payload, target);
clientResponse = targetWithQueryParams(target, buildMultivalueMap(payload)).request(RESPONSE_TYPE).get(Response.class);
}
}
return new RestResponse(clientResponse);
}
private static WebTarget targetWithQueryParams(WebTarget target, MultivaluedMap paramMap) {
for (Entry> param : paramMap.entrySet()) {
target = target.queryParam(param.getKey(), param.getValue());
}
return target;
}
protected boolean isSuccess(Response response) {
int responseStatus = response.getStatus();
return ((responseStatus == 200) || (responseStatus == 201));
}
protected boolean isNew() {
return isNew;
}
protected void setIsNew() {
this.isNew = true;
}
protected synchronized void initialize() {
if (!initialized) {
Response clientResponse = client.target(getRestUrl()).request(RESPONSE_TYPE).get(Response.class);
Map responseMap = Util.processJsonMap(clientResponse.readEntity(String.class));
status = clientResponse.getStatus();
getEntityValues(responseMap);
getChildren(responseMap);
initialized = true;
}
}
protected T getValue(String key, Class clazz) {
initialize();
T retValue = null;
Object value = entityValues.get(key);
if ((value != null) && !(value.equals(JSONObject.NULL))) {
retValue = (T) value;
}
return retValue;
}
protected void setValue(String key, T value) {
initialize();
entityValues.put(key, value);
}
protected Map getEntityMetadata(Map extraProperties) {
Map metadata = new HashMap();
List