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.juneau.rest.httppart;
018
019import static org.apache.juneau.commons.utils.Utils.*;
020
021import java.util.*;
022
023import org.apache.juneau.http.response.*;
024import org.apache.juneau.rest.*;
025
026/**
027 * Represents a single request attribute on an HTTP request.
028 *
029 * <p>
030 * Typically accessed through the {@link RequestAttributes} class.
031 *
032 * <p>
033 *    Some important methods on this class are:
034 * </p>
035 * <ul class='javatree'>
036 *    <li class='jc'>{@link RequestAttribute}
037 *    <ul class='spaced-list'>
038 *       <li>Methods for retrieving simple string values:
039 *       <ul class='javatreec'>
040 *          <li class='jm'>{@link RequestAttribute#asString() asString()}
041 *          <li class='jm'>{@link RequestAttribute#get() get()}
042 *          <li class='jm'>{@link RequestAttribute#isPresent() isPresent()}
043 *          <li class='jm'>{@link RequestAttribute#orElse(Object) orElse(Object)}
044 *       </ul>
045 *       <li>Methods for retrieving as custom types:
046 *       <ul class='javatreec'>
047 *          <li class='jm'>{@link RequestAttribute#as(Class) as(Class)}
048 *       </ul>
049 *       <li>Methods for performing assertion checks:
050 *       <ul class='javatreec'>
051 *          <li class='jm'>{@link RequestAttribute#assertName() assertName()}
052 *          <li class='jm'>{@link RequestAttribute#assertValue() assertValue()}
053 *       </ul>
054 *       <li>Other methods:
055 *       <ul class='javatreec'>
056 *          <li class='jm'>{@link RequestAttribute#getName() getName()}
057 *          <li class='jm'>{@link RequestAttribute#getValue() getValue()}
058*     </ul>
059 * </ul>
060 *
061 * <h5 class='section'>See Also:</h5><ul>
062 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/HttpParts">HTTP Parts</a>
063 * </ul>
064 */
065public class RequestAttribute extends BasicNamedAttribute {
066
067   private final RestRequest req;
068
069   /**
070    * Constructor.
071    *
072    * @param request The request object.
073    * @param name The parameter name.
074    * @param value The parameter value.
075    */
076   public RequestAttribute(RestRequest request, String name, Object value) {
077      super(name, value);
078      this.req = request;
079   }
080
081   /**
082    * Converts this part to the specified POJO.
083    *
084    * @param <T> The type to convert to.
085    * @param type The type to convert to.
086    * @return The converted type, or {@link Optional#empty()} if the part is not present.
087    * @throws BasicHttpException If value could not be parsed.
088    */
089   public <T> Optional<T> as(Class<T> type) {
090      return opt(req.getBeanSession().convertToType(getValue(), type));
091   }
092
093   /**
094    * Returns the value of this part as a string.
095    *
096    * @return The value of this part as a string, or {@link Optional#empty()} if the part was not present.
097    */
098   public Optional<String> asString() {
099      return opt(s(getValue()));
100   }
101}