本文整理汇总了Java中com.amazon.speech.speechlet.authentication.SpeechletRequestSignatureVerifier类的典型用法代码示例。如果您正苦于以下问题:Java SpeechletRequestSignatureVerifier类的具体用法?Java SpeechletRequestSignatureVerifier怎么用?Java SpeechletRequestSignatureVerifier使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SpeechletRequestSignatureVerifier类属于com.amazon.speech.speechlet.authentication包,在下文中一共展示了SpeechletRequestSignatureVerifier类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: doPost
import com.amazon.speech.speechlet.authentication.SpeechletRequestSignatureVerifier; //导入依赖的package包/类
@Override
// A. It must be a POST request.
protected void doPost(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) throws ServletException, IOException {
byte[] serializedSpeechletRequest = IOUtils.toByteArray(httpRequest.getInputStream());
try {
if (this.checkAmazonSignature) {
// B. It must come from the Amazon Alexa cloud.
SpeechletRequestSignatureVerifier.checkRequestSignature(serializedSpeechletRequest,
httpRequest.getHeader(Sdk.SIGNATURE_REQUEST_HEADER),
httpRequest.getHeader(Sdk.SIGNATURE_CERTIFICATE_CHAIN_URL_REQUEST_HEADER));
}
final SpeechletRequestEnvelope<?> requestEnvelope = SpeechletRequestEnvelope.fromJson(serializedSpeechletRequest);
for (SpeechletRequestEnvelopeVerifier verifier : this.requestEnvelopeVerifiers) {
if (!verifier.verify(requestEnvelope)) {
throw new SpeechletRequestHandlerException(this.createExceptionMessage(verifier, requestEnvelope));
}
}
HttpEntity<byte[]> requestEntity = new HttpEntity<>(serializedSpeechletRequest);
ResponseEntity<SpeechletResponseEnvelope> speechletResponse = this.restTemplate.postForEntity(
this.endpoint, requestEntity, SpeechletResponseEnvelope.class);
if (speechletResponse.getStatusCode().is2xxSuccessful() && speechletResponse.hasBody()) {
byte[] outputBytes = speechletResponse.getBody().toJsonBytes();
httpResponse.setContentType("application/json");
httpResponse.setStatus(speechletResponse.getStatusCodeValue());
httpResponse.setContentLength(outputBytes.length);
httpResponse.getOutputStream().write(outputBytes);
} else {
// Should never happen, cause all edge cases are already covered by actual exceptions thrown.
httpResponse.sendError(speechletResponse.getStatusCodeValue(), "Unexpected error in proxy");
}
} catch (Exception e) {
int statusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
if (BAD_REQUEST_EXCEPTION_TYPES.contains(e.getClass())) {
statusCode = HttpServletResponse.SC_BAD_REQUEST;
}
this.log.error("Exception occurred in doPost, returning status code {}", statusCode, e);
httpResponse.sendError(statusCode, e.getClass().getSimpleName() + ": " + e.getMessage());
}
}