下面列出了怎么用org.apache.zookeeper.server.ByteBufferInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
private void validate(List<String> sigs,
ByteBuffer buf) throws SolrException, IOException {
Map<String, byte[]> keys = packageStore.getKeys();
if (keys == null || keys.isEmpty()) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,
"package store does not have any keys");
}
CryptoKeys cryptoKeys = null;
try {
cryptoKeys = new CryptoKeys(keys);
} catch (Exception e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR,
"Error parsing public keys in Package store");
}
for (String sig : sigs) {
if (cryptoKeys.verify(sig, buf) == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Signature does not match any public key : " + sig +" len: "+buf.limit()+ " content sha512: "+
DigestUtils.sha512Hex(new ByteBufferInputStream(buf)));
}
}
}
@SuppressWarnings({"unchecked", "rawtypes"})
public static void waitForAllNodesHaveFile(MiniSolrCloudCluster cluster, String path, Map expected , boolean verifyContent) throws Exception {
for (JettySolrRunner jettySolrRunner : cluster.getJettySolrRunners()) {
String baseUrl = jettySolrRunner.getBaseUrl().toString().replace("/solr", "/api");
String url = baseUrl + "/node/files" + path + "?wt=javabin&meta=true";
assertResponseValues(10, new Fetcher(url, jettySolrRunner), expected);
if(verifyContent) {
try (HttpSolrClient solrClient = (HttpSolrClient) jettySolrRunner.newClient()) {
ByteBuffer buf = Utils.executeGET(solrClient.getHttpClient(), baseUrl + "/node/files" + path,
Utils.newBytesConsumer(Integer.MAX_VALUE));
assertEquals(
"d01b51de67ae1680a84a813983b1de3b592fc32f1a22b662fc9057da5953abd1b72476388ba342cad21671cd0b805503c78ab9075ff2f3951fdf75fa16981420",
DigestUtils.sha512Hex(new ByteBufferInputStream(buf))
);
}
}
}
}
/**
* mapping between column type to java type BIT => BitSet ENUM, YEAR TINY, SHORT, INT24, LONG =>
* int SET, LONGLONG => long FLOAT => float DOUBLE => value NEWDECIMAL => BigDecimal DATE => Date
* TIME, TIME_V2 => Time TIMESTAMP, TIMESTAMP_V2 => Timestmap DATETIME, DATETIME_V2 => Date case
* YEAR: STRING, VARCHAR, VAR_STRING => String BLOB => byte[]
*/
public static Serializable deserializeColumn(
@NonNull final Map<String, ByteBuffer> entity, @NonNull final String column) {
final ByteBuffer byteBuffer = entity.get(column);
if (byteBuffer == null) {
return null;
}
final ByteBufferInputStream inputStream = new ByteBufferInputStream(byteBuffer);
return (Serializable) SerializationUtils.deserialize(inputStream);
}
public static InputStream toJavabin(Object o) throws IOException {
try (final JavaBinCodec jbc = new JavaBinCodec()) {
BinaryRequestWriter.BAOS baos = new BinaryRequestWriter.BAOS();
jbc.marshal(o, baos);
return new ByteBufferInputStream(ByteBuffer.wrap(baos.getbuf(), 0, baos.size()));
}
}
/**
* Creates a JSON string with the metadata
* @lucene.internal
*/
public static MetaData _createJsonMetaData(ByteBuffer buf, List<String> signatures) throws IOException {
String sha512 = DigestUtils.sha512Hex(new ByteBufferInputStream(buf));
Map<String, Object> vals = new HashMap<>();
vals.put(MetaData.SHA512, sha512);
if (signatures != null) {
vals.put("sig", signatures);
}
return new MetaData(vals);
}
@Override
public void submitRequest(Request si)
{
long remaining = firstError != 0 ? LOCKOUT_DURATION_MS - (System.currentTimeMillis() - firstError) : 0;
if ( si.type != ZooDefs.OpCode.createSession && si.type != ZooDefs.OpCode.sync && si.type != ZooDefs.OpCode.ping
&& firstError != 0 && remaining > 0 )
{
log.debug("Rejected : " + si.toString());
// Still reject request
log.debug("Still not ready for " + remaining + "ms");
((NIOServerCnxn)si.cnxn).close();
return;
}
// Submit the request to the legacy Zookeeper server
log.debug("Applied : " + si.toString());
super.submitRequest(si);
// Raise an error if a lock is created
if ( si.type == ZooDefs.OpCode.create )
{
CreateRequest createRequest = new CreateRequest();
try
{
ByteBuffer duplicate = si.request.duplicate();
duplicate.rewind();
ByteBufferInputStream.byteBuffer2Record(duplicate, createRequest);
if ( createRequest.getPath().startsWith(CHAOS_ZNODE_PREFIX)
&& firstError == 0 )
{
firstError = System.currentTimeMillis();
// The znode has been created, close the connection and don't tell it to client
log.warn("Closing connection right after " + createRequest.getPath() + " creation");
((NIOServerCnxn)si.cnxn).close();
}
}
catch ( Exception e )
{
// Should not happen
((NIOServerCnxn)si.cnxn).close();
}
}
}
public InputStream getInputStream() {
if (buf != null) return new ByteBufferInputStream(buf);
return null;
}
@Override
public InputStream getStream() throws IOException {
return new ByteBufferInputStream(baos.getByteBuffer());
}
public BlobContent(String key, ByteBuffer buffer, Decoder<T> decoder) {
this.key = key;
this.content = decoder == null ? (T) buffer : decoder.decode(new ByteBufferInputStream(buffer));
}
@Override
public void submitRequest(Request si)
{
long remaining = firstError != 0 ? LOCKOUT_DURATION_MS - (System.currentTimeMillis() - firstError) : 0;
if ( si.type != ZooDefs.OpCode.createSession && si.type != ZooDefs.OpCode.sync && si.type != ZooDefs.OpCode.ping
&& firstError != 0 && remaining > 0 )
{
log.debug("Rejected : " + si.toString());
// Still reject request
log.debug("Still not ready for " + remaining + "ms");
Compatibility.serverCnxnClose(si.cnxn);
return;
}
// Submit the request to the legacy Zookeeper server
log.debug("Applied : " + si.toString());
super.submitRequest(si);
// Raise an error if a lock is created
if ( (si.type == ZooDefs.OpCode.create) || (si.type == ZooDefs.OpCode.create2) )
{
CreateRequest createRequest = new CreateRequest();
try
{
ByteBuffer duplicate = si.request.duplicate();
duplicate.rewind();
ByteBufferInputStream.byteBuffer2Record(duplicate, createRequest);
if ( createRequest.getPath().startsWith(CHAOS_ZNODE_PREFIX)
&& firstError == 0 )
{
firstError = System.currentTimeMillis();
// The znode has been created, close the connection and don't tell it to client
log.warn("Closing connection right after " + createRequest.getPath() + " creation");
Compatibility.serverCnxnClose(si.cnxn);
}
}
catch ( Exception e )
{
// Should not happen
Compatibility.serverCnxnClose(si.cnxn);
}
}
}