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.server.httppart;
018
019import static org.apache.juneau.commons.utils.Shorts.*;
020
021import java.util.*;
022
023import org.apache.juneau.http.response.*;
024import org.apache.juneau.rest.server.*;
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 * </ul>
061 *
062 * <h2 class='section'>See Also:</h2><ul>
063 *    <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/RestServerHttpParts">HTTP parts in REST servers</a>
064 * </ul>
065 */
066public class RequestAttribute extends BasicNamedAttribute {
067
068   private final RestRequest req;
069
070   /**
071    * Constructor.
072    *
073    * @param request The request object.
074    * @param name The parameter name.
075    * @param value The parameter value.
076    */
077   public RequestAttribute(RestRequest request, String name, Object value) {
078      super(name, value);
079      this.req = request;
080   }
081
082   /**
083    * Converts this part to the specified POJO.
084    *
085    * @param <T> The type to convert to.
086    * @param type The type to convert to.
087    * @return The converted type, or {@link Optional#empty()} if the part is not present.
088    * @throws BasicHttpException If value could not be parsed.
089    */
090   public <T> Optional<T> as(Class<T> type) {
091      return o(req.getMarshallingSession().convertToType(getValue(), type));
092   }
093
094   /**
095    * Returns the value of this part as a string.
096    *
097    * @return The value of this part as a string, or {@link Optional#empty()} if the part was not present.
098    */
099   public Optional<String> asString() {
100      return o(s(getValue()));
101   }
102}