List of usage examples for com.fasterxml.jackson.databind.util ByteBufferBackedInputStream ByteBufferBackedInputStream
public ByteBufferBackedInputStream(ByteBuffer buf)
From source file:com.kixeye.chassis.transport.websocket.RawWebSocketMessage.java
/** * Deserializes the given message.//from www .j a va 2 s . c o m * * @param action * @return * @throws Exception */ public T deserialize(WebSocketAction action) throws Exception { // first deserialize T message = null; if (messageClass != null) { message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass); } // then validate if (message != null && action.shouldValidatePayload()) { SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator); BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName()); validatorAdapter.validate(message, result); if (result.hasErrors()) { throw new MethodArgumentNotValidException( new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result); } } return message; }
From source file:org.apache.usergrid.persistence.qakka.api.QueueResource.java
@ApiOperation(value = "Get data associated with a Queue Message.", response = ApiResponse.class) @ApiResponses(value = { @io.swagger.annotations.ApiResponse(code = 400, message = "Message ID invalid"), @io.swagger.annotations.ApiResponse(code = 404, message = "Queue Message or data not found") }) @GET//w w w. j a v a2 s. c om @Path("{queueName}/data/{queueMessageId}") public Response getMessageData( @ApiParam(value = "Name of Queue", required = true) @PathParam("queueName") String queueName, @ApiParam(value = "ID of Queue Message for which data is to be returned", required = true) @PathParam("queueMessageId") String queueMessageIdParam) { Preconditions.checkArgument(!QakkaUtils.isNullOrEmpty(queueName), "Queue name is required"); UUID queueMessageId; try { queueMessageId = UUID.fromString(queueMessageIdParam); } catch (Exception e) { throw new IllegalArgumentException("Invalid queue message UUID"); } QueueMessage message = queueMessageManager.getMessage(queueName, queueMessageId); if (message == null) { throw new NotFoundException( "Message not found for queueName: " + queueName + " queue message id: " + queueMessageId); } ByteBuffer messageData = queueMessageManager.getMessageData(message.getMessageId()); if (messageData == null) { throw new NotFoundException("Message data not found queueName: " + queueName + " queue message id: " + queueMessageId + " message id: " + message.getMessageId()); } ByteBufferBackedInputStream input = new ByteBufferBackedInputStream(messageData); StreamingOutput stream = output -> { try { ByteStreams.copy(input, output); } catch (Exception e) { throw new WebApplicationException(e); } }; return Response.ok(stream).header("Content-Type", message.getContentType()).build(); }
From source file:strat.mining.stratum.proxy.Launcher.java
/** * Check that a valid SSl certificate already exists. If not, create a new * one.//from www . j av a 2s . c om * * @throws Exception */ private static void checkCertificate() throws Exception { File storeFile = new File(ConfigurationManager.getInstance().getDatabaseDirectory(), KEYSTORE_FILE_NAME); KeyStore keyStore = KeyStore.getInstance("JKS"); if (!storeFile.exists()) { LOGGER.info("KeyStore does not exist. Create {}", storeFile.getAbsolutePath()); storeFile.getParentFile().mkdirs(); storeFile.createNewFile(); keyStore.load(null, null); LOGGER.info("Generating new SSL certificate."); AlgorithmIdentifier sigAlgId = new DefaultSignatureAlgorithmIdentifierFinder().find("SHA256withRSA"); AlgorithmIdentifier digAlgId = new DefaultDigestAlgorithmIdentifierFinder().find(sigAlgId); RSAKeyPairGenerator keyGenerator = new RSAKeyPairGenerator(); keyGenerator .init(new RSAKeyGenerationParameters(BigInteger.valueOf(101), new SecureRandom(), 2048, 14)); AsymmetricCipherKeyPair keysPair = keyGenerator.generateKeyPair(); RSAKeyParameters rsaPrivateKey = (RSAKeyParameters) keysPair.getPrivate(); RSAPrivateKeySpec rsaPrivSpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getExponent()); RSAKeyParameters rsaPublicKey = (RSAKeyParameters) keysPair.getPublic(); RSAPublicKeySpec rsaPublicSpec = new RSAPublicKeySpec(rsaPublicKey.getModulus(), rsaPublicKey.getExponent()); KeyFactory kf = KeyFactory.getInstance("RSA"); PrivateKey rsaPriv = kf.generatePrivate(rsaPrivSpec); PublicKey rsaPub = kf.generatePublic(rsaPublicSpec); X500Name issuerDN = new X500Name("CN=localhost, OU=None, O=None, L=None, C=None"); Integer randomNumber = new SecureRandom().nextInt(); BigInteger serialNumber = BigInteger.valueOf(randomNumber >= 0 ? randomNumber : randomNumber * -1); Date notBefore = new Date(System.currentTimeMillis() - 1000L * 60 * 60 * 24 * 30); Date notAfter = new Date(System.currentTimeMillis() + (1000L * 60 * 60 * 24 * 365 * 10)); X500Name subjectDN = new X500Name("CN=localhost, OU=None, O=None, L=None, C=None"); byte[] publickeyb = rsaPub.getEncoded(); ASN1Sequence sequence = (ASN1Sequence) ASN1Primitive.fromByteArray(publickeyb); SubjectPublicKeyInfo subPubKeyInfo = new SubjectPublicKeyInfo(sequence); X509v3CertificateBuilder v3CertGen = new X509v3CertificateBuilder(issuerDN, serialNumber, notBefore, notAfter, subjectDN, subPubKeyInfo); ContentSigner contentSigner = new BcRSAContentSignerBuilder(sigAlgId, digAlgId) .build(keysPair.getPrivate()); X509CertificateHolder certificateHolder = v3CertGen.build(contentSigner); Certificate certificate = new CertificateFactory() .engineGenerateCertificate(new ByteBufferBackedInputStream( ByteBuffer.wrap(certificateHolder.toASN1Structure().getEncoded()))); LOGGER.info("Certificate generated."); keyStore.setKeyEntry(KEYSTORE_KEY_ENTRY_ALIAS, rsaPriv, KEYSTORE_PASSWORD.toCharArray(), new java.security.cert.Certificate[] { certificate }); keyStore.store(new FileOutputStream(storeFile), KEYSTORE_PASSWORD.toCharArray()); } }