Skip to content

Commit

Permalink
Fix ValidationErrorMessageBodyWriter (#4136)
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam authored and jansupol committed May 28, 2019
1 parent a41590e commit 4787e76
Showing 1 changed file with 18 additions and 10 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2018 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2019 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
Expand Down Expand Up @@ -48,19 +48,17 @@ public boolean isWriteable(final Class<?> type,
return isSupportedMediaType(mediaType) && isSupportedType(type, genericType);
}

private boolean isSupportedType(final Class<?> type, final Type genericType) {
private static boolean isSupportedType(final Class<?> type, final Type genericType) {
if (ValidationError.class.isAssignableFrom(type)) {
return true;
} else if (Collection.class.isAssignableFrom(type)) {
if (genericType instanceof ParameterizedType) {
return ValidationError.class
.isAssignableFrom((Class) ((ParameterizedType) genericType).getActualTypeArguments()[0]);
}
} else if (Collection.class.isAssignableFrom(type) && (genericType instanceof ParameterizedType)) {
return ValidationError.class
.isAssignableFrom((Class) ((ParameterizedType) genericType).getActualTypeArguments()[0]);
}
return false;
}

private boolean isSupportedMediaType(final MediaType mediaType) {
private static boolean isSupportedMediaType(final MediaType mediaType) {
return MediaType.TEXT_HTML_TYPE.equals(mediaType) || MediaType.TEXT_PLAIN_TYPE.equals(mediaType);
}

Expand Down Expand Up @@ -118,7 +116,9 @@ public void writeTo(final Object entity,

// Invalid value.
builder.append(isPlain ? "invalidValue = " : ("<span class=\"invalid-value\"><strong>invalidValue</strong> = "));
builder.append(isPlain ? error.getInvalidValue() : (error.getInvalidValue() + "</span>"));
builder.append(isPlain ? error.getInvalidValue()
: escapeHtml(error.getInvalidValue()).concat("</span>")
);

builder.append(')');

Expand All @@ -137,4 +137,12 @@ public void writeTo(final Object entity,
entityStream.write(builder.toString().getBytes(MessageUtils.getCharset(mediaType)));
entityStream.flush();
}
}

private static final String escapeHtml(String origin) {
return origin == null ? ""
: origin.replaceAll("&", "&amp;")
.replaceAll("\"", "&quot;")
.replaceAll("<", "&lt;")
.replaceAll(">", "&gt;");
}
}

0 comments on commit 4787e76

Please sign in to comment.