001/* 002 * Copyright 2022-2026 Revetware LLC. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.soklet; 018 019import org.jspecify.annotations.NonNull; 020import org.jspecify.annotations.Nullable; 021 022import javax.annotation.concurrent.ThreadSafe; 023import java.util.Map; 024import java.util.Objects; 025import java.util.Optional; 026import java.util.Set; 027 028import static com.soklet.Utilities.trimAggressivelyToEmpty; 029import static com.soklet.Utilities.trimAggressivelyToNull; 030import static java.util.Objects.requireNonNull; 031 032/** 033 * Encapsulates <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS">non-preflight CORS</a> HTTP request data. 034 * <p> 035 * Data for <a href="https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request">preflight</a> requests is represented by {@link CorsPreflight}. 036 * <p> 037 * Instances can be acquired via these factory methods: 038 * <ul> 039 * <li>{@link #fromOrigin(HttpMethod, String)} (uses {@code Origin} header value)</li> 040 * <li>{@link #fromHeaders(HttpMethod, Map)} (parses raw headers)</li> 041 * </ul> 042 * <p> 043 * See <a href="https://www.soklet.com/docs/cors">https://www.soklet.com/docs/cors</a> for detailed documentation. 044 * 045 * @author <a href="https://www.revetkn.com">Mark Allen</a> 046 */ 047@ThreadSafe 048public final class Cors { 049 @Nullable 050 private final HttpMethod httpMethod; 051 @NonNull 052 private final String origin; 053 054 /** 055 * Acquires a CORS <strong>non-preflight</strong> request representation for the given HTTP request data. 056 * 057 * @param httpMethod the request's HTTP method 058 * @param origin HTTP <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin">{@code Origin}</a> request header value 059 * @return a {@link Cors} instance 060 */ 061 @NonNull 062public static Cors fromOrigin(@NonNull HttpMethod httpMethod, 063 @NonNull String origin) { 064 requireNonNull(httpMethod); 065 requireNonNull(origin); 066 067 return new Cors(httpMethod, origin); 068 } 069 070 /** 071 * Extracts a CORS <strong>non-preflight</strong> request representation from the given HTTP request data. 072 * 073 * @param httpMethod the request's HTTP method 074 * @param headers the request headers 075 * @return the CORS non-preflight data for this request, or {@link Optional#empty()} if insufficent data is present 076 */ 077 @NonNull 078 public static Optional<Cors> fromHeaders(@NonNull HttpMethod httpMethod, 079 @NonNull Map<@NonNull String, @NonNull Set<@NonNull String>> headers) { 080 requireNonNull(httpMethod); 081 requireNonNull(headers); 082 083 Set<String> originHeaderValues = headers.entrySet().stream() 084 .filter(e -> e.getKey() != null && "origin".equalsIgnoreCase(trimAggressivelyToEmpty(e.getKey()))) 085 .map(Map.Entry::getValue) 086 .filter(Objects::nonNull) 087 .findFirst() 088 .orElse(null); 089 090 if (originHeaderValues == null || originHeaderValues.size() == 0) 091 return Optional.empty(); 092 093 String originHeaderValue = trimAggressivelyToNull(originHeaderValues.stream().findFirst().orElse(null)); 094 095 if (originHeaderValue == null) 096 return Optional.empty(); 097 098 return Optional.of(new Cors(httpMethod, originHeaderValue)); 099 } 100 101 private Cors(@NonNull HttpMethod httpMethod, 102 @NonNull String origin) { 103 requireNonNull(httpMethod); 104 requireNonNull(origin); 105 106 this.httpMethod = httpMethod; 107 this.origin = origin; 108 } 109 110 @Override 111 @NonNull 112 public String toString() { 113 return String.format("%s{httpMethod=%s, origin=%s}", getClass().getSimpleName(), getHttpMethod().name(), getOrigin()); 114 } 115 116 @Override 117 public boolean equals(@Nullable Object object) { 118 if (this == object) 119 return true; 120 121 if (!(object instanceof Cors cors)) 122 return false; 123 124 return Objects.equals(getHttpMethod(), cors.getHttpMethod()) 125 && Objects.equals(getOrigin(), cors.getOrigin()); 126 } 127 128 @Override 129 public int hashCode() { 130 return Objects.hash(getHttpMethod(), getOrigin()); 131 } 132 133 /** 134 * The HTTP method for this request. 135 * 136 * @return the HTTP method 137 */ 138 @Nullable 139 public HttpMethod getHttpMethod() { 140 return this.httpMethod; 141 } 142 143 /** 144 * The HTTP <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin">{@code Origin}</a> header value for this request. 145 * 146 * @return the header value 147 */ 148 @NonNull 149 public String getOrigin() { 150 return this.origin; 151 } 152}