001/*
002 * Copyright 2016-2024 Ping Identity Corporation
003 *
004 * This program is free software; you can redistribute it and/or modify
005 * it under the terms of the GNU General Public License (GPLv2 only)
006 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
007 * as published by the Free Software Foundation.
008 *
009 * This program is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012 * GNU General Public License for more details.
013 *
014 * You should have received a copy of the GNU General Public License
015 * along with this program; if not, see <http://www.gnu.org/licenses>.
016 */
017
018package com.unboundid.scim2.server.providers;
019
020import com.unboundid.scim2.common.annotations.NotNull;
021import jakarta.annotation.Priority;
022import jakarta.ws.rs.HttpMethod;
023import jakarta.ws.rs.Priorities;
024import jakarta.ws.rs.container.ContainerRequestContext;
025import jakarta.ws.rs.container.ContainerRequestFilter;
026import jakarta.ws.rs.container.PreMatching;
027import jakarta.ws.rs.core.HttpHeaders;
028import jakarta.ws.rs.ext.Provider;
029import java.io.IOException;
030import java.util.Collections;
031
032import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
033
034/**
035 * A ContainerRequestFilter implementation to set the content type header of
036 * POST, PUT, and PATCH requests to "application/scim+json" if the header is
037 * missing.
038 */
039@Provider
040@PreMatching
041@Priority(Priorities.HEADER_DECORATOR)
042public class DefaultContentTypeFilter implements ContainerRequestFilter
043{
044  /**
045   * {@inheritDoc}
046   */
047  public void filter(@NotNull final ContainerRequestContext requestContext)
048      throws IOException
049  {
050    if((requestContext.getMethod().equals(HttpMethod.POST) ||
051        requestContext.getMethod().equals(HttpMethod.PUT) ||
052        requestContext.getMethod().equals("PATCH")) &&
053        requestContext.getMediaType() == null)
054    {
055      requestContext.getHeaders().put(HttpHeaders.CONTENT_TYPE,
056          Collections.singletonList(MEDIA_TYPE_SCIM));
057    }
058  }
059}