protected function finalize($result) {
        $response = Response::create($result);
        $response->meta(['request' => $this->request], true);
        $response->contentTypeFromAccept($this->request->getEnv('HTTP_ACCEPT'));
        $response->contentAsset($this->request->getEnv('HTTP_X_ASSET'));

        $contentType = $response->contentType();

        if ($this->request->getMethod() === Request::METHOD_HEAD) {
            $response->flushHeaders();
            return null;
        }

        // Check for known response types.
        switch ($contentType) {
            case 'application/internal':
                if ($result instanceof \Exception) {
                    throw $result;
                }

                if ($response->contentAsset() === 'response') {
                    return $response;
                } else {
                    return $response->jsonSerialize();
                }
                // No break because everything returns.
            case 'application/json':
                $response->flushHeaders();
                echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
                break;
            default:
                $data = $response->data();
                if (is_string($data)) {
                    $response->flushHeaders();
                    echo $data;
                } else {
                    $response->status(415);
                    $response->flushHeaders();
                    echo "Unsupported response type: $contentType";
                }
                break;
        }
        return null;
    }