This is an automated email from the ASF dual-hosted git repository. ahuber pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/isis.git
The following commit(s) were added to refs/heads/master by this push: new 8d3a7e1 ISIS-1870 fix MediaType parser errors on initialization 8d3a7e1 is described below commit 8d3a7e154589469a5264b04494769b394125f8e0 Author: Andi Huber <ahu...@apache.org> AuthorDate: Thu Feb 22 19:14:10 2018 +0100 ISIS-1870 fix MediaType parser errors on initialization --- .../restfulobjects/applib/RepresentationType.java | 22 ++++++-- .../restfulobjects/applib/util/MediaTypes.java | 58 ++++++++++++++++++++++ 2 files changed, 75 insertions(+), 5 deletions(-) diff --git a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java index 1a6cf5d..3a250ec 100644 --- a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java +++ b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java @@ -44,6 +44,7 @@ import org.apache.isis.viewer.restfulobjects.applib.domaintypes.TypeListRepresen import org.apache.isis.viewer.restfulobjects.applib.errors.ErrorRepresentation; import org.apache.isis.viewer.restfulobjects.applib.homepage.HomePageRepresentation; import org.apache.isis.viewer.restfulobjects.applib.user.UserRepresentation; +import org.apache.isis.viewer.restfulobjects.applib.util.MediaTypes; import org.apache.isis.viewer.restfulobjects.applib.util.Parser; import org.apache.isis.viewer.restfulobjects.applib.version.VersionRepresentation; @@ -139,11 +140,22 @@ public enum RepresentationType { private MediaType xmlMediaType; private final Class<? extends JsonRepresentation> representationClass; - private RepresentationType(final String jsonMediaTypeStr, final String xmlMediaTypeStr, final Class<? extends JsonRepresentation> representationClass) { - this(jsonMediaTypeStr != null ? MediaType.valueOf(jsonMediaTypeStr) : null, xmlMediaTypeStr != null? MediaType.valueOf(xmlMediaTypeStr): null, representationClass); + private RepresentationType( + final String jsonMediaTypeStr, + final String xmlMediaTypeStr, + final Class<? extends JsonRepresentation> representationClass) { + + this( jsonMediaTypeStr != null ? MediaTypes.parse(jsonMediaTypeStr) : null, + xmlMediaTypeStr != null ? MediaTypes.parse(xmlMediaTypeStr) : null, + representationClass + ); } - private RepresentationType(final MediaType jsonMediaType, final MediaType xmlMediaType, final Class<? extends JsonRepresentation> representationClass) { + private RepresentationType( + final MediaType jsonMediaType, + final MediaType xmlMediaType, + final Class<? extends JsonRepresentation> representationClass) { + this.xmlMediaType = xmlMediaType; this.representationClass = representationClass; this.name = Enums.enumToCamelCase(this); @@ -177,14 +189,14 @@ public enum RepresentationType { * parameter value. */ public MediaType getMediaType(String parameter, String paramValue) { - return getMediaType(Collections.singletonMap(parameter, paramValue)); + return getJsonMediaType(Collections.singletonMap(parameter, paramValue)); } /** * Clones the (immutable) {@link #getMediaType() media type}, adding all provided * parameters. * - * @deprecated - use {@link #getMediaType(Map)} instead. + * @deprecated - use {@link #getJsonMediaType(Map)} instead. */ @Deprecated public MediaType getMediaType(Map<String, String> mediaTypeParams) { diff --git a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java new file mode 100644 index 0000000..309dea4 --- /dev/null +++ b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/util/MediaTypes.java @@ -0,0 +1,58 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.isis.viewer.restfulobjects.applib.util; + +import javax.ws.rs.core.MediaType; + +import org.apache.isis.applib.internal.base._Strings; + +public class MediaTypes { + + /** + * Same as {@code MediaType.valueOf(type)}, but with fallback in case {@code MediaType.valueOf(type)} + * throws an IllegalArgumentException. + * <br/><br/> + * The fallback is to retry with String {@code type} cut off at first occurrence of a semicolon (;). + * + * @param type + * @return + */ + public static MediaType parse(String type) { + + if(type==null) + return MediaType.valueOf(null); + + try { + + return MediaType.valueOf(type); + + } catch (IllegalArgumentException e) { + + return _Strings.splitThenStream(type, ";") + .findFirst() + .map(MediaType::valueOf) + .orElseThrow(()->e); // could can't be reached, but re-throw the original exception just in case + + } + + + } + +} -- To stop receiving notification emails like this one, please contact ahu...@apache.org.