Java Code Examples for com.google.protobuf.InvalidProtocolBufferException#toString()

The following examples show how to use com.google.protobuf.InvalidProtocolBufferException#toString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static String slistPut(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    SlistPutRequest slistPutRequest = request.getRequest().unpack(SlistPutRequest.class);
    final LinkedList<SlistEntity> sortedListEntitiesResult = new LinkedList<>();
    final List<SlistProtocol.SlistEntity> sortedListEntities
        = slistPutRequest.getListList();
    for (SlistProtocol.SlistEntity sortedListEntity : sortedListEntities) {
      final String sortedListEntityMember = sortedListEntity.getMember();
      final int sortedListEntityScore = sortedListEntity.getScore();
      sortedListEntitiesResult.add(new SlistEntity(sortedListEntityMember,
          sortedListEntityScore));
    }
    distkvClient.slists().put(request.getKey(), sortedListEntitiesResult);
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 2
Source File: DistkvDictProxy.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String popItem(String key, String itemKey) {
  DistkvResponse response = FutureUtils.get(asyncDictProxy.popItem(key, itemKey));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(DictPopItemResponse.class).getItemValue();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 3
Source File: StreamContext.java    From sawtooth-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Make a Get request on a specific context specified by contextId.
 * @param addresses a collection of address Strings
 * @return Map where the keys are addresses, values Bytestring
 * @throws InternalError               something went wrong processing
 *                                     transaction
 * @throws InvalidTransactionException an invalid transaction was encountered
 */
@Override
public final Map<String, ByteString> getState(final Collection<String> addresses)
    throws InternalError, InvalidTransactionException {
  TpStateGetRequest getRequest = TpStateGetRequest.newBuilder().addAllAddresses(addresses)
      .setContextId(this.contextId).build();
  Future future = stream.send(Message.MessageType.TP_STATE_GET_REQUEST, getRequest.toByteString());
  TpStateGetResponse getResponse = null;
  try {
    getResponse = TpStateGetResponse.parseFrom(future.getResult(TIME_OUT));
  } catch (InterruptedException iee) {
    throw new InternalError(iee.toString());
  } catch (InvalidProtocolBufferException ipbe) {
    // server didn't respond with a GetResponse
    throw new InternalError(ipbe.toString());
  } catch (ValidatorConnectionError vce) {
    throw new InternalError(vce.toString());
  } catch (Exception e) {
    throw new InternalError(e.toString());
  }
  Map<String, ByteString> results = new HashMap<String, ByteString>();
  if (getResponse != null) {
    if (getResponse.getStatus() == TpStateGetResponse.Status.AUTHORIZATION_ERROR) {
      throw new InvalidTransactionException("Tried to get unauthorized address " + addresses.toString());
    }
    for (TpStateEntry entry : getResponse.getEntriesList()) {
      results.put(entry.getAddress(), entry.getData());
    }
  }

  return results;
}
 
Example 4
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String slistTop(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    SlistTopRequest slistTopRequest = request.getRequest().unpack(SlistTopRequest.class);
    final StringBuilder stringBuilder = new StringBuilder();
    LinkedList<SlistEntity> listEntities = distkvClient.slists()
        .top(request.getKey(), slistTopRequest.getCount());
    boolean first = true;
    stringBuilder.append("[");
    for (final SlistEntity entity : listEntities) {
      if (first) {
        first = false;
      } else {
        stringBuilder.append(", ");
      }
      stringBuilder.append("(");
      stringBuilder.append(entity.getMember());
      stringBuilder.append(", ");
      stringBuilder.append(entity.getScore());
      stringBuilder.append(")");
    }
    stringBuilder.append("]");
    return stringBuilder.toString();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }

}
 
Example 5
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String dictRemoveItem(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    DictRemoveItemRequest dictRemoveItemRequest =
        request.getRequest().unpack(DictRemoveItemRequest.class);
    distkvClient.dicts().removeItem(request.getKey(), dictRemoveItemRequest.getItemKey());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 6
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String dictPopItem(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    DictPopItemRequest dictPopItemRequest = request.getRequest().unpack(DictPopItemRequest.class);
    return distkvClient.dicts().popItem(request.getKey(), dictPopItemRequest.getItemKey());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 7
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String intGet(
    DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    return String.valueOf(distkvClient.ints().get(request.getKey()));
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 8
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String dictPutItem(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    DictPutItemRequest dictPutItemRequest = request.getRequest().unpack(DictPutItemRequest.class);
    distkvClient.dicts().putItem(
        request.getKey(), dictPutItemRequest.getItemKey(), dictPutItemRequest.getItemValue());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 9
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String dictPut(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    DictPutRequest dictPutRequest = request.getRequest().unpack(DictPutRequest.class);
    DistKVDict dict = dictPutRequest.getDict();
    Map<String, String> map = new HashMap<>();
    for (int i = 0; i < dict.getKeysCount(); i++) {
      map.put(dict.getKeys(i), dict.getValues(i));
    }
    distkvClient.dicts().put(request.getKey(), map);
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 10
Source File: StreamContext.java    From sawtooth-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * Make a Set request on a specific context specified by contextId.
 * @param addressValuePairs A collection of Map.Entry's
 * @return addressesThatWereSet, A collection of address Strings that were set
 * @throws InternalError               something went wrong processing
 *                                     transaction
 * @throws InvalidTransactionException an invalid transaction was encountered
 */
@Override
public final Collection<String> setState(final Collection<java.util.Map.Entry<String, ByteString>> addressValuePairs)
    throws InternalError, InvalidTransactionException {
  ArrayList<TpStateEntry> entryArrayList = new ArrayList<TpStateEntry>();
  for (Map.Entry<String, ByteString> entry : addressValuePairs) {
    TpStateEntry ourTpStateEntry = TpStateEntry.newBuilder().setAddress(entry.getKey()).setData(entry.getValue())
        .build();
    entryArrayList.add(ourTpStateEntry);
  }
  TpStateSetRequest setRequest = TpStateSetRequest.newBuilder().addAllEntries(entryArrayList)
      .setContextId(this.contextId).build();
  Future future = stream.send(Message.MessageType.TP_STATE_SET_REQUEST, setRequest.toByteString());
  TpStateSetResponse setResponse = null;
  try {
    setResponse = TpStateSetResponse.parseFrom(future.getResult(TIME_OUT));
  } catch (InterruptedException iee) {
    throw new InternalError(iee.toString());

  } catch (InvalidProtocolBufferException ipbe) {
    // server didn't respond with a SetResponse
    throw new InternalError(ipbe.toString());
  } catch (ValidatorConnectionError vce) {
    throw new InternalError(vce.toString());
  } catch (Exception e) {
    throw new InternalError(e.toString());
  }
  ArrayList<String> addressesThatWereSet = new ArrayList<String>();
  if (setResponse != null) {
    if (setResponse.getStatus() == TpStateSetResponse.Status.AUTHORIZATION_ERROR) {
      throw new InvalidTransactionException("Tried to set unauthorized address " + addressValuePairs.toString());
    }
    for (String address : setResponse.getAddressesList()) {
      addressesThatWereSet.add(address);
    }
  }

  return addressesThatWereSet;
}
 
Example 11
Source File: DistkvSetProxy.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public boolean exists(String key, String entity) {
  DistkvResponse response = FutureUtils.get(asyncSetProxy.exists(key, entity));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(SetExistsResponse.class).getResult();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 12
Source File: DistkvListProxy.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List<String> get(String key, Integer index) {
  DistkvResponse response = FutureUtils.get(asyncListProxy.get(key, index));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(ListGetResponse.class).getValuesList();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 13
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String intPut(
    DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    IntPutRequest intPutRequest = request.getRequest().unpack(IntPutRequest.class);
    distkvClient.ints().put(request.getKey(), intPutRequest.getValue());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 14
Source File: DistkvDictProxy.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public String getItem(String key, String itemKey) {
  DistkvResponse response = FutureUtils.get(asyncDictProxy.getItem(key, itemKey));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(DictGetItemResponse.class).getItemValue();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 15
Source File: StreamContext.java    From sawtooth-sdk-java with Apache License 2.0 5 votes vote down vote up
@Override
public final void addEvent(final String eventType, final Collection<Entry<String, String>> attributes,
    final ByteString data) throws InternalError {
  List<Attribute> attList = new ArrayList<>();
  for (Map.Entry<String, String> entry : attributes) {
    Attribute att = Attribute.newBuilder().setKey(entry.getKey()).setValue(entry.getValue()).build();
    attList.add(att);
  }
  Builder evtBuilder = Event.newBuilder().addAllAttributes(attList).setEventType(eventType);
  if (data != null) {
    evtBuilder.setData(data);
  }
  Event evt = evtBuilder.build();
  TpEventAddRequest evtAddRequest = TpEventAddRequest.newBuilder().setContextId(contextId).setEvent(evt).build();

  Future future = stream.send(Message.MessageType.TP_EVENT_ADD_REQUEST, evtAddRequest.toByteString());
  TpEventAddResponse evtAddResponse = null;
  try {
    evtAddResponse = TpEventAddResponse.parseFrom(future.getResult(TIME_OUT));
  } catch (InterruptedException iee) {
    throw new InternalError(iee.toString());
  } catch (InvalidProtocolBufferException ipbe) {
    // server didn't respond with a EventAddResponse
    throw new InternalError(ipbe.toString());
  } catch (ValidatorConnectionError vce) {
    throw new InternalError(vce.toString());
  } catch (Exception e) {
    throw new InternalError(e.toString());
  }
  if (evtAddResponse != null) {
    if (evtAddResponse.getStatus() == TpEventAddResponse.Status.ERROR) {
      throw new InternalError(String.format("Failed to add event %s, %s, %s", eventType, attributes, data));
    }
  }
}
 
Example 16
Source File: DefaultDistkvClient.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public long ttl(String key) {
  DistkvResponse response = FutureUtils.get(asyncClient.ttl(key));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(TTLResponse.class).getTtl();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 17
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String slistGetMember(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    SlistGetMemberRequest slistGetMemberRequest =
        request.getRequest().unpack(SlistGetMemberRequest.class);
    final DistkvTuple<Integer, Integer> tuple =
        distkvClient.slists().getMember(request.getKey(), slistGetMemberRequest.getMember());
    // output: (member, score), ranking
    final StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append("(");
    stringBuilder.append(slistGetMemberRequest.getMember());
    stringBuilder.append(", ");
    stringBuilder.append(tuple.getFirst());
    stringBuilder.append("), ");
    final int ranking = tuple.getSecond();
    stringBuilder.append(ranking);
    switch (ranking) {
      case 1:
        stringBuilder.append("st");
        break;
      case 2:
        stringBuilder.append("nd");
        break;
      case 3:
        stringBuilder.append("rd");
        break;
      default:
        stringBuilder.append("th");
    }
    return stringBuilder.toString();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 18
Source File: DistkvListProxy.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public List<String> get(String key, Integer from, Integer end) {
  DistkvResponse response = FutureUtils.get(asyncListProxy.get(key, from, end));
  CheckStatusUtil.checkStatus(response.getStatus(), key, typeCode);
  try {
    return response.getResponse().unpack(ListGetResponse.class).getValuesList();
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}
 
Example 19
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String listPut(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    ListPutRequest listPutRequest = request.getRequest().unpack(ListPutRequest.class);
    distkvClient.lists().put(request.getKey(), listPutRequest.getValuesList());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
  return STATUS_OK;
}
 
Example 20
Source File: CommandExecutorHandler.java    From distkv with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public static String strGet(DistkvClient distkvClient, DistkvParsedResult parsedResult) {
  try {
    DistkvRequest request = parsedResult.getRequest();
    return distkvClient.strs().get(request.getKey());
  } catch (InvalidProtocolBufferException e) {
    throw new DistkvException(e.toString());
  }
}