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