| // *************************************************************************************************************************** |
| // * 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.juneau.rest; |
| |
| import static java.util.Optional.*; |
| import static org.apache.juneau.internal.StringUtils.*; |
| |
| import java.util.*; |
| |
| import org.apache.juneau.http.response.*; |
| |
| /** |
| * Represents a single request attribute on an HTTP request. |
| */ |
| public class RequestAttribute extends BasicNamedAttribute { |
| |
| private final RestRequest req; |
| |
| /** |
| * Constructor. |
| * |
| * @param request The request object. |
| * @param name The parameter name. |
| * @param value The parameter value. |
| */ |
| public RequestAttribute(RestRequest request, String name, Object value) { |
| super(name, value); |
| this.req = request; |
| } |
| |
| //------------------------------------------------------------------------------------------------------------------ |
| // Retrievers |
| //------------------------------------------------------------------------------------------------------------------ |
| |
| /** |
| * Returns the value of this part as a string. |
| * |
| * @return The value of this part as a string, or {@link Optional#empty()} if the part was not present. |
| */ |
| public Optional<String> asString() { |
| return ofNullable(stringify(getValue())); |
| } |
| |
| /** |
| * Converts this part to the specified POJO. |
| * |
| * @param <T> The type to convert to. |
| * @param type The type to convert to. |
| * @return The converted type, or {@link Optional#empty()} if the part is not present. |
| * @throws BasicHttpException If value could not be parsed. |
| */ |
| public <T> Optional<T> asType(Class<T> type) { |
| return ofNullable(req.getBeanSession().convertToType(getValue(), type)); |
| } |
| |
| //------------------------------------------------------------------------------------------------------------------ |
| // Header passthrough methods. |
| //------------------------------------------------------------------------------------------------------------------ |
| |
| // <FluentSetters> |
| |
| // </FluentSetters> |
| } |