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

Fix ValidationErrorMessageBodyWriter #4136

Merged
merged 1 commit into from
May 28, 2019
Merged
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
@@ -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;");
}
}