View Javadoc

1   package org.apache.helix.controller.restlet;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.StringReader;
23  import java.util.Map;
24  import java.util.TreeMap;
25  
26  import org.apache.log4j.Logger;
27  import org.codehaus.jackson.map.ObjectMapper;
28  import org.codehaus.jackson.type.TypeReference;
29  import org.restlet.data.Form;
30  import org.restlet.data.MediaType;
31  import org.restlet.data.Status;
32  import org.restlet.resource.Representation;
33  import org.restlet.resource.Resource;
34  
35  /**
36   * REST resource for ZkPropertyTransfer server to receive PUT requests 
37   * that submits ZNRecordUpdates
38   * */
39  public class ZNRecordUpdateResource  extends Resource
40  {
41    public static final String UPDATEKEY = "ZNRecordUpdate";
42    private static Logger LOG = Logger.getLogger(ZNRecordUpdateResource.class);
43    @Override
44    public boolean allowGet()
45    {
46      return false;
47    }
48  
49    @Override
50    public boolean allowPost()
51    {
52      return false;
53    }
54  
55    @Override
56    public boolean allowPut()
57    {
58      return true;
59    }
60  
61    @Override
62    public boolean allowDelete()
63    {
64      return false;
65    }
66    
67    @Override
68    public void storeRepresentation(Representation entity)
69    {
70      try
71      {
72        ZKPropertyTransferServer server = ZKPropertyTransferServer.getInstance();
73        
74        Form form = new Form(entity);
75        String jsonPayload = form.getFirstValue(UPDATEKEY, true);
76        
77        // Parse the map from zkPath --> ZNRecordUpdate from the payload
78        StringReader sr = new StringReader(jsonPayload);
79        ObjectMapper mapper = new ObjectMapper();
80        TypeReference<TreeMap<String, ZNRecordUpdate>> typeRef =
81            new TypeReference<TreeMap<String, ZNRecordUpdate>>()
82            {
83            };
84        Map<String, ZNRecordUpdate> holderMap = mapper.readValue(sr, typeRef);
85        // Enqueue the ZNRecordUpdate for sending
86        for(ZNRecordUpdate holder : holderMap.values())
87        {
88          server.enqueueData(holder);
89          LOG.info("Received " + holder.getPath() + " from " + getRequest().getClientInfo().getAddress());
90        }
91        getResponse().setStatus(Status.SUCCESS_OK);
92      }
93      catch(Exception e)
94      {
95        LOG.error("", e);
96        getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
97      }
98    }
99  }