001/** 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.webserver; 020 021import org.apache.avro.io.*; 022import org.apache.avro.specific.SpecificDatumReader; 023import org.apache.avro.specific.SpecificDatumWriter; 024 025import javax.servlet.ServletException; 026import java.io.ByteArrayOutputStream; 027import java.io.IOException; 028import java.nio.ByteBuffer; 029 030/** 031 * Serialize Http Request Response data with Avro. 032 */ 033public class AvroHttpSerializer { 034 035 public static final String JSON_CHARSET = "ISO-8859-1"; 036 037 public AvroHttpSerializer() { 038 } 039 040 /** 041 * Convert from HttpServletRequest to AvroHttpRequest. 042 */ 043 public AvroHttpRequest toAvro(final ParsedHttpRequest parsedRequest) throws ServletException, IOException { 044 return AvroHttpRequest.newBuilder() 045 .setRequestUrl(parsedRequest.getRequestUrl()) 046 .setHttpMethod(parsedRequest.getMethod()) 047 .setQueryString(parsedRequest.getQueryString()) 048 .setPathInfo(parsedRequest.getPathInfo()) 049 .setInputStream(ByteBuffer.wrap(parsedRequest.getInputStream())) 050 .build(); 051 } 052 053 /** 054 * Convert AvroHttpRequest to JSON String. 055 */ 056 public String toString(final AvroHttpRequest request) { 057 final DatumWriter<AvroHttpRequest> configurationWriter = new SpecificDatumWriter<>(AvroHttpRequest.class); 058 try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { 059 final JsonEncoder encoder = EncoderFactory.get().jsonEncoder(AvroHttpRequest.SCHEMA$, out); 060 configurationWriter.write(request, encoder); 061 encoder.flush(); 062 return out.toString(JSON_CHARSET); 063 } catch (final IOException e) { 064 throw new RuntimeException(e); 065 } 066 } 067 068 /** 069 * Convert AvroHttpRequest to bytes. 070 */ 071 public byte[] toBytes(final AvroHttpRequest request) { 072 final DatumWriter<AvroHttpRequest> requestWriter = new SpecificDatumWriter<>(AvroHttpRequest.class); 073 try (final ByteArrayOutputStream out = new ByteArrayOutputStream()) { 074 final BinaryEncoder encoder = EncoderFactory.get().binaryEncoder(out, null); 075 requestWriter.write(request, encoder); 076 encoder.flush(); 077 return out.toByteArray(); 078 } catch (final IOException e) { 079 throw new RuntimeException(e); 080 } 081 } 082 083 /** 084 * Convert JSON String to AvroHttpRequest. 085 */ 086 public AvroHttpRequest fromString(final String jasonStr) { 087 try { 088 final JsonDecoder decoder = DecoderFactory.get().jsonDecoder(AvroHttpRequest.getClassSchema(), jasonStr); 089 final SpecificDatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class); 090 return reader.read(null, decoder); 091 } catch (final IOException e) { 092 throw new RuntimeException(e); 093 } 094 } 095 096 /** 097 * Convert bytes to AvroHttpRequest. 098 */ 099 public AvroHttpRequest fromBytes(final byte[] theBytes) { 100 try { 101 final BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(theBytes, null); 102 final SpecificDatumReader<AvroHttpRequest> reader = new SpecificDatumReader<>(AvroHttpRequest.class); 103 return reader.read(null, decoder); 104 } catch (final IOException e) { 105 throw new RuntimeException(e); 106 } 107 } 108}