{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE StrictData #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# OPTIONS_GHC -fno-warn-unused-imports #-}
{-# OPTIONS_GHC -fno-warn-unused-matches #-}

-- Derived from AWS service descriptions, licensed under Apache 2.0.

-- |
-- Module      : Amazonka.WAFV2.Types.CustomResponse
-- Copyright   : (c) 2013-2023 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay
-- Stability   : auto-generated
-- Portability : non-portable (GHC extensions)
module Amazonka.WAFV2.Types.CustomResponse where

import qualified Amazonka.Core as Core
import qualified Amazonka.Core.Lens.Internal as Lens
import qualified Amazonka.Data as Data
import qualified Amazonka.Prelude as Prelude
import Amazonka.WAFV2.Types.CustomHTTPHeader

-- | A custom response to send to the client. You can define a custom
-- response for rule actions and default web ACL actions that are set to
-- BlockAction.
--
-- For information about customizing web requests and responses, see
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-custom-request-response.html Customizing web requests and responses in WAF>
-- in the
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
--
-- /See:/ 'newCustomResponse' smart constructor.
data CustomResponse = CustomResponse'
  { -- | References the response body that you want WAF to return to the web
    -- request client. You can define a custom response for a rule action or a
    -- default web ACL action that is set to block. To do this, you first
    -- define the response body key and value in the @CustomResponseBodies@
    -- setting for the WebACL or RuleGroup where you want to use it. Then, in
    -- the rule action or web ACL default action @BlockAction@ setting, you
    -- reference the response body using this key.
    CustomResponse -> Maybe Text
customResponseBodyKey :: Prelude.Maybe Prelude.Text,
    -- | The HTTP headers to use in the response. Duplicate header names are not
    -- allowed.
    --
    -- For information about the limits on count and size for custom request
    -- and response settings, see
    -- <https://docs.aws.amazon.com/waf/latest/developerguide/limits.html WAF quotas>
    -- in the
    -- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
    CustomResponse -> Maybe (NonEmpty CustomHTTPHeader)
responseHeaders :: Prelude.Maybe (Prelude.NonEmpty CustomHTTPHeader),
    -- | The HTTP status code to return to the client.
    --
    -- For a list of status codes that you can use in your custom responses,
    -- see
    -- <https://docs.aws.amazon.com/waf/latest/developerguide/customizing-the-response-status-codes.html Supported status codes for custom response>
    -- in the
    -- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
    CustomResponse -> Natural
responseCode :: Prelude.Natural
  }
  deriving (CustomResponse -> CustomResponse -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CustomResponse -> CustomResponse -> Bool
$c/= :: CustomResponse -> CustomResponse -> Bool
== :: CustomResponse -> CustomResponse -> Bool
$c== :: CustomResponse -> CustomResponse -> Bool
Prelude.Eq, ReadPrec [CustomResponse]
ReadPrec CustomResponse
Int -> ReadS CustomResponse
ReadS [CustomResponse]
forall a.
(Int -> ReadS a)
-> ReadS [a] -> ReadPrec a -> ReadPrec [a] -> Read a
readListPrec :: ReadPrec [CustomResponse]
$creadListPrec :: ReadPrec [CustomResponse]
readPrec :: ReadPrec CustomResponse
$creadPrec :: ReadPrec CustomResponse
readList :: ReadS [CustomResponse]
$creadList :: ReadS [CustomResponse]
readsPrec :: Int -> ReadS CustomResponse
$creadsPrec :: Int -> ReadS CustomResponse
Prelude.Read, Int -> CustomResponse -> ShowS
[CustomResponse] -> ShowS
CustomResponse -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CustomResponse] -> ShowS
$cshowList :: [CustomResponse] -> ShowS
show :: CustomResponse -> String
$cshow :: CustomResponse -> String
showsPrec :: Int -> CustomResponse -> ShowS
$cshowsPrec :: Int -> CustomResponse -> ShowS
Prelude.Show, forall x. Rep CustomResponse x -> CustomResponse
forall x. CustomResponse -> Rep CustomResponse x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall x. Rep CustomResponse x -> CustomResponse
$cfrom :: forall x. CustomResponse -> Rep CustomResponse x
Prelude.Generic)

-- |
-- Create a value of 'CustomResponse' with all optional fields omitted.
--
-- Use <https://hackage.haskell.org/package/generic-lens generic-lens> or <https://hackage.haskell.org/package/optics optics> to modify other optional fields.
--
-- The following record fields are available, with the corresponding lenses provided
-- for backwards compatibility:
--
-- 'customResponseBodyKey', 'customResponse_customResponseBodyKey' - References the response body that you want WAF to return to the web
-- request client. You can define a custom response for a rule action or a
-- default web ACL action that is set to block. To do this, you first
-- define the response body key and value in the @CustomResponseBodies@
-- setting for the WebACL or RuleGroup where you want to use it. Then, in
-- the rule action or web ACL default action @BlockAction@ setting, you
-- reference the response body using this key.
--
-- 'responseHeaders', 'customResponse_responseHeaders' - The HTTP headers to use in the response. Duplicate header names are not
-- allowed.
--
-- For information about the limits on count and size for custom request
-- and response settings, see
-- <https://docs.aws.amazon.com/waf/latest/developerguide/limits.html WAF quotas>
-- in the
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
--
-- 'responseCode', 'customResponse_responseCode' - The HTTP status code to return to the client.
--
-- For a list of status codes that you can use in your custom responses,
-- see
-- <https://docs.aws.amazon.com/waf/latest/developerguide/customizing-the-response-status-codes.html Supported status codes for custom response>
-- in the
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
newCustomResponse ::
  -- | 'responseCode'
  Prelude.Natural ->
  CustomResponse
newCustomResponse :: Natural -> CustomResponse
newCustomResponse Natural
pResponseCode_ =
  CustomResponse'
    { $sel:customResponseBodyKey:CustomResponse' :: Maybe Text
customResponseBodyKey =
        forall a. Maybe a
Prelude.Nothing,
      $sel:responseHeaders:CustomResponse' :: Maybe (NonEmpty CustomHTTPHeader)
responseHeaders = forall a. Maybe a
Prelude.Nothing,
      $sel:responseCode:CustomResponse' :: Natural
responseCode = Natural
pResponseCode_
    }

-- | References the response body that you want WAF to return to the web
-- request client. You can define a custom response for a rule action or a
-- default web ACL action that is set to block. To do this, you first
-- define the response body key and value in the @CustomResponseBodies@
-- setting for the WebACL or RuleGroup where you want to use it. Then, in
-- the rule action or web ACL default action @BlockAction@ setting, you
-- reference the response body using this key.
customResponse_customResponseBodyKey :: Lens.Lens' CustomResponse (Prelude.Maybe Prelude.Text)
customResponse_customResponseBodyKey :: Lens' CustomResponse (Maybe Text)
customResponse_customResponseBodyKey = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CustomResponse' {Maybe Text
customResponseBodyKey :: Maybe Text
$sel:customResponseBodyKey:CustomResponse' :: CustomResponse -> Maybe Text
customResponseBodyKey} -> Maybe Text
customResponseBodyKey) (\s :: CustomResponse
s@CustomResponse' {} Maybe Text
a -> CustomResponse
s {$sel:customResponseBodyKey:CustomResponse' :: Maybe Text
customResponseBodyKey = Maybe Text
a} :: CustomResponse)

-- | The HTTP headers to use in the response. Duplicate header names are not
-- allowed.
--
-- For information about the limits on count and size for custom request
-- and response settings, see
-- <https://docs.aws.amazon.com/waf/latest/developerguide/limits.html WAF quotas>
-- in the
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
customResponse_responseHeaders :: Lens.Lens' CustomResponse (Prelude.Maybe (Prelude.NonEmpty CustomHTTPHeader))
customResponse_responseHeaders :: Lens' CustomResponse (Maybe (NonEmpty CustomHTTPHeader))
customResponse_responseHeaders = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CustomResponse' {Maybe (NonEmpty CustomHTTPHeader)
responseHeaders :: Maybe (NonEmpty CustomHTTPHeader)
$sel:responseHeaders:CustomResponse' :: CustomResponse -> Maybe (NonEmpty CustomHTTPHeader)
responseHeaders} -> Maybe (NonEmpty CustomHTTPHeader)
responseHeaders) (\s :: CustomResponse
s@CustomResponse' {} Maybe (NonEmpty CustomHTTPHeader)
a -> CustomResponse
s {$sel:responseHeaders:CustomResponse' :: Maybe (NonEmpty CustomHTTPHeader)
responseHeaders = Maybe (NonEmpty CustomHTTPHeader)
a} :: CustomResponse) forall b c a. (b -> c) -> (a -> b) -> a -> c
Prelude.. forall (f :: * -> *) (g :: * -> *) s t a b.
(Functor f, Functor g) =>
AnIso s t a b -> Iso (f s) (g t) (f a) (g b)
Lens.mapping forall s t a b. (Coercible s a, Coercible t b) => Iso s t a b
Lens.coerced

-- | The HTTP status code to return to the client.
--
-- For a list of status codes that you can use in your custom responses,
-- see
-- <https://docs.aws.amazon.com/waf/latest/developerguide/customizing-the-response-status-codes.html Supported status codes for custom response>
-- in the
-- <https://docs.aws.amazon.com/waf/latest/developerguide/waf-chapter.html WAF Developer Guide>.
customResponse_responseCode :: Lens.Lens' CustomResponse Prelude.Natural
customResponse_responseCode :: Lens' CustomResponse Natural
customResponse_responseCode = forall s a b t. (s -> a) -> (s -> b -> t) -> Lens s t a b
Lens.lens (\CustomResponse' {Natural
responseCode :: Natural
$sel:responseCode:CustomResponse' :: CustomResponse -> Natural
responseCode} -> Natural
responseCode) (\s :: CustomResponse
s@CustomResponse' {} Natural
a -> CustomResponse
s {$sel:responseCode:CustomResponse' :: Natural
responseCode = Natural
a} :: CustomResponse)

instance Data.FromJSON CustomResponse where
  parseJSON :: Value -> Parser CustomResponse
parseJSON =
    forall a. String -> (Object -> Parser a) -> Value -> Parser a
Data.withObject
      String
"CustomResponse"
      ( \Object
x ->
          Maybe Text
-> Maybe (NonEmpty CustomHTTPHeader) -> Natural -> CustomResponse
CustomResponse'
            forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"CustomResponseBodyKey")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser (Maybe a)
Data..:? Key
"ResponseHeaders")
            forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
Prelude.<*> (Object
x forall a. FromJSON a => Object -> Key -> Parser a
Data..: Key
"ResponseCode")
      )

instance Prelude.Hashable CustomResponse where
  hashWithSalt :: Int -> CustomResponse -> Int
hashWithSalt Int
_salt CustomResponse' {Natural
Maybe (NonEmpty CustomHTTPHeader)
Maybe Text
responseCode :: Natural
responseHeaders :: Maybe (NonEmpty CustomHTTPHeader)
customResponseBodyKey :: Maybe Text
$sel:responseCode:CustomResponse' :: CustomResponse -> Natural
$sel:responseHeaders:CustomResponse' :: CustomResponse -> Maybe (NonEmpty CustomHTTPHeader)
$sel:customResponseBodyKey:CustomResponse' :: CustomResponse -> Maybe Text
..} =
    Int
_salt
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe Text
customResponseBodyKey
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Maybe (NonEmpty CustomHTTPHeader)
responseHeaders
      forall a. Hashable a => Int -> a -> Int
`Prelude.hashWithSalt` Natural
responseCode

instance Prelude.NFData CustomResponse where
  rnf :: CustomResponse -> ()
rnf CustomResponse' {Natural
Maybe (NonEmpty CustomHTTPHeader)
Maybe Text
responseCode :: Natural
responseHeaders :: Maybe (NonEmpty CustomHTTPHeader)
customResponseBodyKey :: Maybe Text
$sel:responseCode:CustomResponse' :: CustomResponse -> Natural
$sel:responseHeaders:CustomResponse' :: CustomResponse -> Maybe (NonEmpty CustomHTTPHeader)
$sel:customResponseBodyKey:CustomResponse' :: CustomResponse -> Maybe Text
..} =
    forall a. NFData a => a -> ()
Prelude.rnf Maybe Text
customResponseBodyKey
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Maybe (NonEmpty CustomHTTPHeader)
responseHeaders
      seq :: forall a b. a -> b -> b
`Prelude.seq` forall a. NFData a => a -> ()
Prelude.rnf Natural
responseCode

instance Data.ToJSON CustomResponse where
  toJSON :: CustomResponse -> Value
toJSON CustomResponse' {Natural
Maybe (NonEmpty CustomHTTPHeader)
Maybe Text
responseCode :: Natural
responseHeaders :: Maybe (NonEmpty CustomHTTPHeader)
customResponseBodyKey :: Maybe Text
$sel:responseCode:CustomResponse' :: CustomResponse -> Natural
$sel:responseHeaders:CustomResponse' :: CustomResponse -> Maybe (NonEmpty CustomHTTPHeader)
$sel:customResponseBodyKey:CustomResponse' :: CustomResponse -> Maybe Text
..} =
    [Pair] -> Value
Data.object
      ( forall a. [Maybe a] -> [a]
Prelude.catMaybes
          [ (Key
"CustomResponseBodyKey" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe Text
customResponseBodyKey,
            (Key
"ResponseHeaders" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..=)
              forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
Prelude.<$> Maybe (NonEmpty CustomHTTPHeader)
responseHeaders,
            forall a. a -> Maybe a
Prelude.Just (Key
"ResponseCode" forall kv v. (KeyValue kv, ToJSON v) => Key -> v -> kv
Data..= Natural
responseCode)
          ]
      )