View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.rest.repr.formats;
21  
22  import org.neo4j.server.rest.repr.BadInputException;
23  import org.neo4j.server.rest.repr.ListWriter;
24  import org.neo4j.server.rest.repr.MappingWriter;
25  import org.neo4j.server.rest.repr.RepresentationFormat;
26  
27  import javax.ws.rs.core.MediaType;
28  import java.io.UnsupportedEncodingException;
29  import java.net.URI;
30  import java.net.URLDecoder;
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  public class UrlFormFormat extends RepresentationFormat
37  {
38      public UrlFormFormat()
39      {
40          super( MediaType.APPLICATION_FORM_URLENCODED_TYPE );
41      }
42  
43      @Override
44      protected String serializeValue( final String type, final Object value )
45      {
46          throw new RuntimeException( "Not implemented!" );
47      }
48  
49      @Override
50      protected ListWriter serializeList( final String type )
51      {
52          throw new RuntimeException( "Not implemented!" );
53      }
54  
55      @Override
56      protected MappingWriter serializeMapping( final String type )
57      {
58          throw new RuntimeException( "Not implemented!" );
59      }
60  
61      @Override
62      protected String complete( final ListWriter serializer )
63      {
64          throw new RuntimeException( "Not implemented!" );
65      }
66  
67      @Override
68      protected String complete( final MappingWriter serializer )
69      {
70          throw new RuntimeException( "Not implemented!" );
71      }
72  
73      @Override
74      public Object readValue( final String input ) throws BadInputException
75      {
76          throw new RuntimeException( "Not implemented!" );
77      }
78  
79      @Override
80      public Map<String, Object> readMap( final String input ) throws BadInputException
81      {
82          HashMap<String, Object> result = new HashMap<String, Object>();
83          if ( input.isEmpty() )
84          {
85              return result;
86          }
87  
88          for ( String pair : input.split( "\\&" ) )
89          {
90              String[] fields = pair.split( "=" );
91              String key;
92              String value;
93  
94              try
95              {
96                  key = URLDecoder.decode( fields[ 0 ], "UTF-8" ); //Shouldn't this come from the outside?
97                  value = URLDecoder.decode( fields[ 1 ], "UTF-8" );
98              } catch ( UnsupportedEncodingException e )
99              {
100                 throw new BadInputException( e );
101             }
102 
103             Object old = result.get( key );
104             if ( old == null )
105             {
106                 result.put( key, value );
107             } else
108             {
109                 List<Object> list;
110                 if ( old instanceof List<?> )
111                 {
112                     list = (List<Object>)old;
113                 } else
114                 {
115                     list = new ArrayList<Object>();
116                     result.put(key,list);
117                     list.add( old );
118                 }
119                 list.add( value );
120             }
121         }
122 
123 
124         return result;
125     }
126 
127     @Override
128     public List<Object> readList( final String input ) throws BadInputException
129     {
130         throw new RuntimeException( "Not implemented!" );
131     }
132 
133     @Override
134     public URI readUri( final String input ) throws BadInputException
135     {
136         throw new RuntimeException( "Not implemented!" );
137     }
138 }