View Javadoc

1   /**
2    *       Copyright 2010 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   */
21  
22  package org.apache.amber.oauth2.ext.dynamicreg.server.validator;
23  
24  import org.apache.amber.oauth2.common.OAuth;
25  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
26  import org.apache.amber.oauth2.common.utils.OAuthUtils;
27  import org.apache.amber.oauth2.common.validators.AbstractValidator;
28  import org.apache.amber.oauth2.ext.dynamicreg.common.OAuthRegistration;
29  import org.apache.amber.oauth2.ext.dynamicreg.server.request.JSONHttpServletRequestWrapper;
30  import org.slf4j.Logger;
31  import org.slf4j.LoggerFactory;
32  
33  /**
34   *
35   *
36   *
37   */
38  public class PushPullValidator extends AbstractValidator<JSONHttpServletRequestWrapper> {
39  
40      private Logger log = LoggerFactory.getLogger(PushPullValidator.class);
41  
42      public PushPullValidator() {
43  
44      }
45  
46      public void validateContentType(JSONHttpServletRequestWrapper request) throws OAuthProblemException {
47  
48          String contentType = request.getContentType();
49          final String expectedContentType = OAuth.ContentType.JSON;
50          if (!OAuthUtils.hasContentType(contentType, expectedContentType)) {
51              throw OAuthUtils.handleBadContentTypeException(expectedContentType);
52          }
53  
54  //        if content type is json initialize validator for pull or push method based on json content
55          initializeValidationParameter(request);
56  
57      }
58  
59      private void initializeValidationParameter(JSONHttpServletRequestWrapper request)
60          throws OAuthProblemException {
61          final String requestType = request.getParameter(OAuthRegistration.Request.TYPE);
62  
63          if (OAuthUtils.isEmpty(requestType)) {
64              throw OAuthUtils.handleOAuthProblemException("Missing [type] parameter value");
65          }
66  
67          if (OAuthRegistration.Type.PULL.equals(requestType)) {
68              requiredParams.add(OAuthRegistration.Request.TYPE);
69              requiredParams.add(OAuthRegistration.Request.CLIENT_URL);
70          } else if (OAuthRegistration.Type.PUSH.equals(requestType)) {
71              requiredParams.add(OAuthRegistration.Request.TYPE);
72              requiredParams.add(OAuthRegistration.Request.CLIENT_NAME);
73              requiredParams.add(OAuthRegistration.Request.CLIENT_URL);
74              requiredParams.add(OAuthRegistration.Request.CLIENT_DESCRIPTION);
75              requiredParams.add(OAuthRegistration.Request.REDIRECT_URL);
76          } else {
77              throw OAuthUtils.handleOAuthProblemException("Invalid [type] parameter value");
78          }
79  
80          if (log.isDebugEnabled()) {
81              log.debug("OAuth dynamic client registration type is: {}", new String[] {requestType});
82          }
83      }
84  }