public static < T extends AlexaSpeechlet > T createSpeechletFromRequest ( final byte [ ] serializedSpeechletRequest , final Class < T > speechletClass , final UtteranceReader utteranceReader ) throws IOException { final ObjectMapper mapper = new ObjectMapper ( ) ; final JsonNode parser = mapper . readTree ( serializedSpeechletRequest ) ; final String locale = Optional . of ( parser . path ( "request" ) ) . filter ( node -> ! node . isMissingNode ( ) ) . map ( node -> node . path ( "locale" ) ) . filter ( node -> ! node . isMissingNode ( ) ) . map ( JsonNode :: textValue ) . orElse ( DEFAULT_LOCALE ) ; try { return speechletClass . getConstructor ( String . class , UtteranceReader . class ) . newInstance ( locale , utteranceReader ) ; } catch ( InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e ) { throw new IOException ( "Could not create Speechlet from speechlet request" , e ) ; } }
Creates an AlexaSpeechlet from bytes of a speechlet request . It will extract the locale from the request and uses it for creating a new instance of AlexaSpeechlet