001/** 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.camel.component.extension.verifier; 018 019import java.util.Collection; 020import java.util.Collections; 021import java.util.HashMap; 022import java.util.HashSet; 023import java.util.Map; 024import java.util.Optional; 025import java.util.Set; 026import java.util.function.Supplier; 027 028import org.apache.camel.component.extension.ComponentVerifierExtension.VerificationError; 029import org.apache.camel.util.ObjectHelper; 030 031public final class ResultErrorBuilder { 032 private VerificationError.Code code; 033 private String description; 034 private Set<String> parameters; 035 private Map<VerificationError.Attribute, Object> attributes; 036 037 public ResultErrorBuilder() { 038 } 039 040 // ********************************** 041 // Accessors 042 // ********************************** 043 044 public ResultErrorBuilder code(VerificationError.Code code) { 045 this.code = code; 046 return this; 047 } 048 049 public ResultErrorBuilder code(String code) { 050 code(VerificationError.asCode(code)); 051 return this; 052 } 053 054 public ResultErrorBuilder description(String description) { 055 this.description = description; 056 return this; 057 } 058 059 public ResultErrorBuilder parameterKey(String parameter) { 060 if (parameter != null) { 061 if (this.parameters == null) { 062 this.parameters = new HashSet<>(); 063 } 064 065 this.parameters.add(parameter); 066 } 067 return this; 068 } 069 070 public ResultErrorBuilder parameterKeys(Collection<String> parameterList) { 071 if (parameterList != null) { 072 parameterList.forEach(this::parameterKey); 073 } 074 075 return this; 076 } 077 078 public ResultErrorBuilder detail(String key, Object value) { 079 detail(VerificationError.asAttribute(key), value); 080 return this; 081 } 082 083 public ResultErrorBuilder detail(VerificationError.Attribute key, Object value) { 084 if (value != null) { 085 if (this.attributes == null) { 086 this.attributes = new HashMap<>(); 087 } 088 089 this.attributes.put(key, value); 090 } 091 return this; 092 } 093 094 public <T> ResultErrorBuilder detail(String key, Supplier<Optional<T>> supplier) { 095 detail(VerificationError.asAttribute(key), supplier); 096 return this; 097 } 098 099 public <T> ResultErrorBuilder detail(VerificationError.Attribute key, Supplier<Optional<T>> supplier) { 100 supplier.get().ifPresent(value -> detail(key, value)); 101 return this; 102 } 103 104 public ResultErrorBuilder details(Map<VerificationError.Attribute, Object> details) { 105 for (Map.Entry<VerificationError.Attribute, Object> entry : details.entrySet()) { 106 detail(entry.getKey(), entry.getValue()); 107 } 108 109 return this; 110 } 111 112 // ********************************** 113 // Build 114 // ********************************** 115 116 public VerificationError build() { 117 return new DefaultResultVerificationError( 118 code, 119 description, 120 parameters != null ? Collections.unmodifiableSet(parameters) : Collections.emptySet(), 121 attributes != null ? Collections.unmodifiableMap(attributes) : Collections.emptyMap() 122 ); 123 } 124 125 // ********************************** 126 // Helpers 127 // ********************************** 128 129 public static ResultErrorBuilder fromError(VerificationError error) { 130 return new ResultErrorBuilder() 131 .code(error.getCode()) 132 .description(error.getDescription()) 133 .parameterKeys(error.getParameterKeys()) 134 .details(error.getDetails()); 135 } 136 137 public static ResultErrorBuilder withCode(VerificationError.Code code) { 138 return new ResultErrorBuilder().code(code); 139 } 140 141 public static ResultErrorBuilder withCode(String code) { 142 return new ResultErrorBuilder().code(code); 143 } 144 145 public static ResultErrorBuilder withHttpCode(int code) { 146 return withCode(convertHttpCodeToErrorCode(code)) 147 .detail(VerificationError.HttpAttribute.HTTP_CODE, code); 148 } 149 150 public static ResultErrorBuilder withHttpCodeAndText(int code, String text) { 151 return withCodeAndDescription(convertHttpCodeToErrorCode(code), text) 152 .detail(VerificationError.HttpAttribute.HTTP_CODE, code) 153 .detail(VerificationError.HttpAttribute.HTTP_TEXT, text); 154 } 155 156 private static VerificationError.StandardCode convertHttpCodeToErrorCode(int code) { 157 return code >= 400 && code < 500 ? VerificationError.StandardCode.AUTHENTICATION : VerificationError.StandardCode.GENERIC; 158 } 159 160 public static ResultErrorBuilder withCodeAndDescription(VerificationError.Code code, String description) { 161 return new ResultErrorBuilder().code(code).description(description); 162 } 163 164 public static ResultErrorBuilder withUnsupportedScope(String scope) { 165 return new ResultErrorBuilder() 166 .code(VerificationError.StandardCode.UNSUPPORTED_SCOPE) 167 .description("Unsupported scope: " + scope); 168 } 169 170 public static ResultErrorBuilder withUnsupportedComponent(String component) { 171 return new ResultErrorBuilder() 172 .code(VerificationError.StandardCode.UNSUPPORTED_COMPONENT) 173 .description("Unsupported component: " + component); 174 } 175 176 public static ResultErrorBuilder withException(Exception exception) { 177 return new ResultErrorBuilder() 178 .code(VerificationError.StandardCode.EXCEPTION) 179 .description(exception.getMessage()) 180 .detail(VerificationError.ExceptionAttribute.EXCEPTION_INSTANCE, exception) 181 .detail(VerificationError.ExceptionAttribute.EXCEPTION_CLASS, exception.getClass().getName()); 182 } 183 184 public static ResultErrorBuilder withMissingOption(String optionName) { 185 return new ResultErrorBuilder() 186 .code(VerificationError.StandardCode.MISSING_PARAMETER) 187 .description(optionName + " should be set") 188 .parameterKey(optionName); 189 } 190 191 public static ResultErrorBuilder withUnknownOption(String optionName) { 192 return new ResultErrorBuilder() 193 .code(VerificationError.StandardCode.UNKNOWN_PARAMETER) 194 .description("Unknown option " + optionName) 195 .parameterKey(optionName); 196 } 197 198 public static ResultErrorBuilder withIllegalOption(String optionName) { 199 return new ResultErrorBuilder() 200 .code(VerificationError.StandardCode.ILLEGAL_PARAMETER) 201 .description("Illegal option " + optionName) 202 .parameterKey(optionName); 203 } 204 205 public static ResultErrorBuilder withIllegalOption(String optionName, String optionValue) { 206 return ObjectHelper.isNotEmpty(optionValue) 207 ? new ResultErrorBuilder() 208 .code(VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE) 209 .description(optionName + " has wrong value (" + optionValue + ")") 210 .parameterKey(optionName) 211 : withIllegalOption(optionName); 212 } 213}