001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.isis.viewer.restfulobjects.rendering.util; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.nio.charset.StandardCharsets; 024 025import com.fasterxml.jackson.core.JsonParseException; 026import com.fasterxml.jackson.databind.JsonMappingException; 027 028import org.apache.isis.core.commons.internal.base._Bytes; 029import org.apache.isis.core.commons.internal.base._Strings; 030import org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation; 031import org.apache.isis.viewer.restfulobjects.applib.RestfulResponse; 032import org.apache.isis.viewer.restfulobjects.applib.util.JsonMapper; 033import org.apache.isis.viewer.restfulobjects.rendering.RestfulObjectsApplicationException; 034 035public final class Util { 036 037 private Util(){} 038 039 // ////////////////////////////////////////////////////////////// 040 // parsing 041 // ////////////////////////////////////////////////////////////// 042 043 /** 044 * Parse {@link java.io.InputStream} to String, else throw exception 045 */ 046 public static String asStringUtf8(final InputStream body) { 047 try { 048 return _Strings.ofBytes(_Bytes.of(body), StandardCharsets.UTF_8); 049 } catch (final IOException e) { 050 throw RestfulObjectsApplicationException.createWithCauseAndMessage(RestfulResponse.HttpStatusCode.BAD_REQUEST, e, "could not read body"); 051 } 052 } 053 054 /** 055 * Parse (body) string to {@link org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation}, else throw exception 056 */ 057 public static JsonRepresentation readAsMap(final String body) { 058 if (body == null) { 059 return JsonRepresentation.newMap(); 060 } 061 final String bodyTrimmed = body.trim(); 062 if (bodyTrimmed.isEmpty()) { 063 return JsonRepresentation.newMap(); 064 } 065 return read(bodyTrimmed, "body"); 066 } 067 068 /** 069 * Parse (query) string to {@link org.apache.isis.viewer.restfulobjects.applib.JsonRepresentation}, else throw exception 070 */ 071 public static JsonRepresentation readQueryStringAsMap(final String queryString) { 072 if (queryString == null) { 073 return JsonRepresentation.newMap(); 074 } 075 final String queryStringTrimmed = queryString.trim(); 076 if (queryStringTrimmed.isEmpty()) { 077 return JsonRepresentation.newMap(); 078 } 079 return read(queryStringTrimmed, "query string"); 080 } 081 082 private static JsonRepresentation read(final String args, final String argsNature) { 083 try { 084 final JsonRepresentation jsonRepr = JsonMapper.instance().read(args); 085 if (!jsonRepr.isMap()) { 086 throw RestfulObjectsApplicationException.createWithMessage(RestfulResponse.HttpStatusCode.BAD_REQUEST, "could not read %s as a JSON map", argsNature); 087 } 088 return jsonRepr; 089 } catch (final JsonParseException e) { 090 throw RestfulObjectsApplicationException.createWithCauseAndMessage(RestfulResponse.HttpStatusCode.BAD_REQUEST, e, "could not parse %s", argsNature); 091 } catch (final JsonMappingException e) { 092 throw RestfulObjectsApplicationException.createWithCauseAndMessage(RestfulResponse.HttpStatusCode.BAD_REQUEST, e, "could not read %s as JSON", argsNature); 093 } catch (final IOException e) { 094 throw RestfulObjectsApplicationException.createWithCauseAndMessage(RestfulResponse.HttpStatusCode.BAD_REQUEST, e, "could not parse %s", argsNature); 095 } 096 } 097 098 099}