View Javadoc
1   /*
2    * Copyright 2006-2011 the original author or authors.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
5    * the License. You may obtain a copy of the License at
6    * 
7    * https://www.apache.org/licenses/LICENSE-2.0
8    * 
9    * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
10   * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
11   * specific language governing permissions and limitations under the License.
12   */
13  package org.springframework.security.oauth2.common.exceptions;
14  
15  import java.io.IOException;
16  import java.util.Map.Entry;
17  
18  import com.fasterxml.jackson.core.JsonGenerator;
19  import com.fasterxml.jackson.core.JsonProcessingException;
20  import com.fasterxml.jackson.databind.SerializerProvider;
21  import com.fasterxml.jackson.databind.ser.std.StdSerializer;
22  
23  /**
24   * @author Brian Clozel
25   *
26   */
27  public class OAuth2ExceptionJackson2Serializer extends StdSerializer<OAuth2Exception> {
28  
29      public OAuth2ExceptionJackson2Serializer() {
30          super(OAuth2Exception.class);
31      }
32  
33  	@Override
34  	public void serialize(OAuth2Exception value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
35  			JsonProcessingException {
36          jgen.writeStartObject();
37  		jgen.writeStringField("error", value.getOAuth2ErrorCode());
38  		jgen.writeStringField("error_description", value.getMessage());
39  		if (value.getAdditionalInformation()!=null) {
40  			for (Entry<String, String> entry : value.getAdditionalInformation().entrySet()) {
41  				String key = entry.getKey();
42  				String add = entry.getValue();
43  				jgen.writeStringField(key, add);				
44  			}
45  		}
46          jgen.writeEndObject();
47  	}
48  
49  }