Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PAYARA-2378 JSON Parsing issues #2377

Merged
merged 7 commits into from
Feb 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public SseResponseBody addHeader(String name, Object value) {

@Override
public JsonObject toJson() throws JsonException {
JsonObject json = Json.createObjectBuilder(super.toJson()).build();
JsonObject json = super.toJson();

if (!headers.isEmpty()) {
JsonObject o = Json.createObjectBuilder().build();
Expand All @@ -92,7 +92,7 @@ public JsonObject toJson() throws JsonException {
JsonUtil.accumalate(o, key, JsonUtil.getJsonValue(value));
}
}
JsonUtil.accumalate(json, "headers", o);
json = JsonUtil.accumalate(json, "headers", o);
}

return json;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import java.util.Properties;
import javax.json.JsonException;
import javax.json.JsonObject;
import javax.json.Json;
import javax.json.JsonObjectBuilder;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
Expand All @@ -65,7 +65,7 @@
public class AdminCommandStateCmdResultJsonProvider extends AdminCommandStateJsonProvider {

@Override
protected void addActionReporter(ActionReporter ar, JsonObject json) throws JsonException {
protected void addActionReporter(ActionReporter ar, JsonObjectBuilder json) throws JsonException {
if (ar != null) {
CommandResult cr = CompositeUtil.instance().getModel(CommandResult.class);
cr.setMessage(ar.getMessage());
Expand All @@ -78,7 +78,7 @@ protected void addActionReporter(ActionReporter ar, JsonObject json) throws Json
}
cr.setExtraProperties(map);
}
json.put("command-result", (JsonObject) JsonUtil.getJsonValue(cr));
json.add("command-result", (JsonObject) JsonUtil.getJsonValue(cr));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ public JsonObject processState(AdminCommandState state) throws JsonException {
result.add("id", state.getId());
result.add("empty-payload", state.isOutboundPayloadEmpty());
ActionReporter ar = (ActionReporter) state.getActionReport();
addActionReporter(ar, result.build());
addActionReporter(ar, result);
return result.build();
}

protected void addActionReporter(ActionReporter ar, JsonObject json) throws JsonException {
protected void addActionReporter(ActionReporter ar, JsonObjectBuilder json) throws JsonException {
if (ar != null) {
json.put("action-report", actionReportJsonProvider.processReport(ar));
json.add("action-report", actionReportJsonProvider.processReport(ar));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
* only if the new code is made subject to such option by the copyright
* holder.
*
* Portions Copyright [2017] Payara Foundation and/or affiliates
*/
// Portions Copyright [2017-2018] Payara Foundation and/or affiliates

package org.glassfish.admin.rest.provider;

Expand All @@ -47,7 +47,6 @@
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;
import java.util.Iterator;
import java.util.Set;
import java.util.logging.Level;
import javax.json.Json;
Expand Down Expand Up @@ -93,11 +92,8 @@ public String getContent(OptionsResult proxy) {
public JsonArray getRespresenationForMethodMetaData(OptionsResult proxy) {
JsonArrayBuilder arr = Json.createArrayBuilder();
Set<String> methods = proxy.methods();
Iterator<String> iterator = methods.iterator();
String methodName;
while (iterator.hasNext()) {
for (String methodName : methods){
try {
methodName = iterator.next();
MethodMetaData methodMetaData = proxy.getMethodMetaData(methodName);
JsonObjectBuilder method = Json.createObjectBuilder();
method.add(NAME, methodName);
Expand All @@ -108,7 +104,6 @@ public JsonArray getRespresenationForMethodMetaData(OptionsResult proxy) {
RestLogging.restLogger.log(Level.SEVERE, null, ex);
}
}

return arr.build();
}

Expand All @@ -131,24 +126,20 @@ public JsonArray getRespresenationForMethodMetaData(OptionsResult proxy) {

private JsonObject getParameter(ParameterMetaData parameterMetaData) throws JsonException {
JsonObjectBuilder result = Json.createObjectBuilder();
Iterator<String> iterator = parameterMetaData.attributes().iterator();
String attributeName;
while (iterator.hasNext()) {
attributeName = iterator.next();

for (String attributeName: parameterMetaData.attributes()){
result.add(attributeName, parameterMetaData.getAttributeValue(attributeName));
}

return result.build();
}

private JsonObject getMessageParams(MethodMetaData methodMetaData) throws JsonException {
JsonObjectBuilder result = Json.createObjectBuilder();
if (methodMetaData.sizeParameterMetaData() > 0) {
Set<String> parameters = methodMetaData.parameters();
Iterator<String> iterator = parameters.iterator();
String parameter;
while (iterator.hasNext()) {
parameter = iterator.next();
ParameterMetaData parameterMetaData = methodMetaData.getParameterMetaData(parameter);
for (String parameter: parameters){
ParameterMetaData parameterMetaData = methodMetaData.getParameterMetaData(parameter);
result.add(parameter, getParameter(parameterMetaData));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
Expand Down Expand Up @@ -104,6 +106,7 @@ public ParameterMap readFrom(Class<ParameterMap> type, Type genericType,
return map;

} catch (Exception ex) {
Logger.getLogger("org.glassfish.admin.rest").log(Level.SEVERE, null, ex);
ParameterMap map = new ParameterMap();
map.add("error", "Entity Parsing Error: " + ex.getMessage());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,17 @@ private static Confidential getConfidentialAnnotation(Class clazz, String getter
}
}

/**
* Converts a {@link Collection} of {@link JsonValue}s or other Json-compatible types into a {@link JsonAray}
* @param c
* @return
* @throws JsonException
*/
public static JsonArray processCollection(Collection c) throws JsonException {
JsonArrayBuilder result = Json.createArrayBuilder();
Iterator i = c.iterator();
while (i.hasNext()) {
JsonValue item = getJsonValue(i.next());
for (Object item: c){
result.add(JsonUtil.getJsonValue(item));
}

return result.build();
}

Expand Down Expand Up @@ -292,38 +295,6 @@ public static int getInt(JsonObject jsonObject, String key, int dflt) {
return dflt;
}
}

/**
* Puts a value into the specified jsonObject
* <p>
* This method is synchronised on the jsonObject
* @param jsonObject
* @param key
* @param value
*/
public static void put(JsonObject jsonObject, String key, Object value) {
try {
synchronized(jsonObject) {
jsonObject.put(key, value!=null?getJsonValue(value):JsonObject.NULL);
}
} catch (JsonException e) {
// ignore. The exception is thrown only if the value is non-finite number
// or if the key is null.
}
}

/**
* Puts a {@link JsonObject} into a {@link JsonArray}
* <p>
* This method is synchronised on the jsonArray
* @param jsonArray
* @param item
*/
public static void put(JsonArray jsonArray, JsonObject item) {
synchronized(jsonArray) {
jsonArray.add(item);
}
}

/**
* Puts a value into a {@link JsonObject} with the specified key.
Expand All @@ -333,21 +304,26 @@ public static void put(JsonArray jsonArray, JsonObject item) {
* @param jsonObject
* @param key
* @param value
* @return
* @since 5.0
*/
public static void accumalate(JsonObject jsonObject, String key, JsonValue value){
public static JsonObject accumalate(JsonObject jsonObject, String key, JsonValue value){
JsonObjectBuilder jsonBuilder = Json.createObjectBuilder(jsonObject);
if (jsonObject.containsKey(key)){
JsonValue previous = jsonObject.get(key);
if (previous instanceof JsonArray){
((JsonArray) previous).add(value);
JsonArrayBuilder prev = Json.createArrayBuilder((JsonArray)previous);
prev.add(value);
jsonBuilder.add(key, prev);
} else {
JsonArrayBuilder arrayBuilder = Json.createArrayBuilder();
arrayBuilder.add(previous);
arrayBuilder.add(value);
jsonObject.put(key, arrayBuilder.build());
jsonBuilder.add(key, arrayBuilder);
}
} else {
jsonObject.put(key, value);
jsonBuilder.add(key, value);
}
return jsonBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
* only if the new code is made subject to such option by the copyright
* holder.
*
* Portions Copyright [2017] Payara Foundation and/or affiliates
*/
// Portions Copyright [2017-2018] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.remote.reader;

import com.sun.enterprise.admin.remote.AdminCommandStateImpl;
Expand Down Expand Up @@ -83,6 +84,7 @@ public AdminCommandState readFrom(final InputStream is, final String contentType
String str = baos.toString("UTF-8");
try {
JsonParser parser = Json.createParser(new StringReader(str));
parser.next();
JsonObject json = parser.getObject();
return readAdminCommandState(json);
} catch (JsonException ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.remote.reader;

import java.io.IOException;
Expand Down Expand Up @@ -67,8 +69,7 @@ public ProgressStatusDTO readFrom(final HttpURLConnection urlConnection) throws

@Override
public ProgressStatusDTO readFrom(final InputStream is, final String contentType) throws IOException {
JsonParser jp = factory.createJsonParser(is);
try {
try (JsonParser jp = factory.createJsonParser(is)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just as a comment, but when you see uncommon abbreviations like "jp" here, best to rename it to something more clear right away

JsonToken token = jp.nextToken(); //sorounding object
jp.nextToken(); //Name progress-status
JsonToken token2 = jp.nextToken();
Expand All @@ -78,8 +79,6 @@ public ProgressStatusDTO readFrom(final InputStream is, final String contentType
throw new IOException("Not expected type (progress-status) but (" + jp.getCurrentName() + ")");
}
return readProgressStatus(jp);
} finally {
jp.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
* only if the new code is made subject to such option by the copyright
* holder.
*/
// Portions Copyright [2018] Payara Foundation and/or affiliates

package com.sun.enterprise.admin.remote.reader;

import java.io.IOException;
Expand Down Expand Up @@ -70,8 +72,7 @@ public ProgressStatusEvent readFrom(HttpURLConnection urlConnection) throws IOEx

@Override
public ProgressStatusEvent readFrom(final InputStream is, final String contentType) throws IOException {
JsonParser jp = factory.createJsonParser(is);
try {
try (JsonParser jp = factory.createJsonParser(is)) {
JsonToken token = jp.nextToken(); //sorounding object
jp.nextToken(); //Name progress-status-event
JsonToken token2 = jp.nextToken();
Expand All @@ -81,8 +82,6 @@ public ProgressStatusEvent readFrom(final InputStream is, final String contentTy
throw new IOException("Not expected type (progress-status-event) but (" + jp.getCurrentName() + ")");
}
return readProgressStatusEvent(jp);
} finally {
jp.close();
}
}

Expand Down