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    * http://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 org.codehaus.jackson.JsonGenerator;
19  import org.codehaus.jackson.JsonProcessingException;
20  import org.codehaus.jackson.map.JsonSerializer;
21  import org.codehaus.jackson.map.SerializerProvider;
22  
23  /**
24   * @author Dave Syer
25   *
26   */
27  public class OAuth2ExceptionJackson1Serializer extends JsonSerializer<OAuth2Exception> {
28  
29  	@Override
30  	public void serialize(OAuth2Exception value, JsonGenerator jgen, SerializerProvider provider) throws IOException,
31  			JsonProcessingException {
32          jgen.writeStartObject();
33  		jgen.writeStringField("error", value.getOAuth2ErrorCode());
34  		jgen.writeStringField("error_description", value.getMessage());
35  		if (value.getAdditionalInformation()!=null) {
36  			for (Entry<String, String> entry : value.getAdditionalInformation().entrySet()) {
37  				String key = entry.getKey();
38  				String add = entry.getValue();
39  				jgen.writeStringField(key, add);				
40  			}
41  		}
42          jgen.writeEndObject();
43  	}
44  
45  }