001/*
002 * Copyright 2016-2019 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 javax.annotation.Priority;
021import javax.ws.rs.HttpMethod;
022import javax.ws.rs.Priorities;
023import javax.ws.rs.container.ContainerRequestContext;
024import javax.ws.rs.container.ContainerRequestFilter;
025import javax.ws.rs.container.PreMatching;
026import javax.ws.rs.core.HttpHeaders;
027import javax.ws.rs.ext.Provider;
028import java.io.IOException;
029import java.util.Collections;
030
031import static com.unboundid.scim2.common.utils.ApiConstants.MEDIA_TYPE_SCIM;
032
033/**
034 * A ContainerRequestFilter implementation to set the content type header of
035 * POST, PUT, and PATCH requests to "application/scim+json" if the header is
036 * missing.
037 */
038@Provider
039@PreMatching
040@Priority(Priorities.HEADER_DECORATOR)
041public class DefaultContentTypeFilter implements ContainerRequestFilter
042{
043  /**
044   * {@inheritDoc}
045   */
046  public void filter(final ContainerRequestContext requestContext)
047      throws IOException
048  {
049    if((requestContext.getMethod().equals(HttpMethod.POST) ||
050        requestContext.getMethod().equals(HttpMethod.PUT) ||
051        requestContext.getMethod().equals("PATCH")) &&
052        requestContext.getMediaType() == null)
053    {
054      requestContext.getHeaders().put(HttpHeaders.CONTENT_TYPE,
055          Collections.singletonList(MEDIA_TYPE_SCIM));
056    }
057  }
058}