Package org.apache.helix.controller.restlet

Source Code of org.apache.helix.controller.restlet.ZNRecordUpdateResource

package org.apache.helix.controller.restlet;

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import java.io.StringReader;
import java.util.Map;
import java.util.TreeMap;

import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;
import org.restlet.data.Form;
import org.restlet.data.MediaType;
import org.restlet.data.Status;
import org.restlet.representation.Representation;
import org.restlet.representation.Variant;
import org.restlet.resource.ServerResource;

/**
* REST resource for ZkPropertyTransfer server to receive PUT requests
* that submits ZNRecordUpdates
*/
public class ZNRecordUpdateResource extends ServerResource {
  public static final String UPDATEKEY = "ZNRecordUpdate";
  private static Logger LOG = Logger.getLogger(ZNRecordUpdateResource.class);

  public ZNRecordUpdateResource() {
    getVariants().add(new Variant(MediaType.TEXT_PLAIN));
    getVariants().add(new Variant(MediaType.APPLICATION_JSON));
    setNegotiated(false);
  }

  @Override
  public Representation put(Representation entity) {
    try {
      ZKPropertyTransferServer server = ZKPropertyTransferServer.getInstance();

      Form form = new Form(entity);
      String jsonPayload = form.getFirstValue(UPDATEKEY, true);

      // Parse the map from zkPath --> ZNRecordUpdate from the payload
      StringReader sr = new StringReader(jsonPayload);
      ObjectMapper mapper = new ObjectMapper();
      TypeReference<TreeMap<String, ZNRecordUpdate>> typeRef =
          new TypeReference<TreeMap<String, ZNRecordUpdate>>() {
          };
      Map<String, ZNRecordUpdate> holderMap = mapper.readValue(sr, typeRef);
      // Enqueue the ZNRecordUpdate for sending
      for (ZNRecordUpdate holder : holderMap.values()) {
        server.enqueueData(holder);
        LOG.info("Received " + holder.getPath() + " from "
            + getRequest().getClientInfo().getAddress());
      }
      getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
      LOG.error("", e);
      getResponse().setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return null;
  }

}
TOP

Related Classes of org.apache.helix.controller.restlet.ZNRecordUpdateResource

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.