github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/common/tools/configtxlator/rest/protolator_handlers.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package rest
    18  
    19  import (
    20  	"bytes"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"net/http"
    24  	"reflect"
    25  
    26  	"github.com/hyperledger/fabric/common/tools/protolator"
    27  
    28  	// Import these to register the proto types
    29  	_ "github.com/hyperledger/fabric/protos/common"
    30  	_ "github.com/hyperledger/fabric/protos/msp"
    31  	_ "github.com/hyperledger/fabric/protos/orderer"
    32  	_ "github.com/hyperledger/fabric/protos/peer"
    33  
    34  	"github.com/golang/protobuf/proto"
    35  	"github.com/gorilla/mux"
    36  )
    37  
    38  func getMsgType(r *http.Request) (proto.Message, error) {
    39  	vars := mux.Vars(r)
    40  	msgName := vars["msgName"] // Will not arrive is unset
    41  
    42  	msgType := proto.MessageType(msgName)
    43  	if msgType == nil {
    44  		return nil, fmt.Errorf("message name not found")
    45  	}
    46  	return reflect.New(msgType.Elem()).Interface().(proto.Message), nil
    47  }
    48  
    49  func Decode(w http.ResponseWriter, r *http.Request) {
    50  	msg, err := getMsgType(r)
    51  	if err != nil {
    52  		w.WriteHeader(http.StatusNotFound)
    53  		fmt.Fprintln(w, err)
    54  		return
    55  	}
    56  
    57  	buf, err := ioutil.ReadAll(r.Body)
    58  	if err != nil {
    59  		w.WriteHeader(http.StatusBadRequest)
    60  		fmt.Fprintln(w, err)
    61  		return
    62  	}
    63  
    64  	err = proto.Unmarshal(buf, msg)
    65  	if err != nil {
    66  		w.WriteHeader(http.StatusBadRequest)
    67  		fmt.Fprintln(w, err)
    68  		return
    69  	}
    70  
    71  	var buffer bytes.Buffer
    72  	err = protolator.DeepMarshalJSON(&buffer, msg)
    73  	if err != nil {
    74  		w.WriteHeader(http.StatusBadRequest)
    75  		fmt.Fprintln(w, err)
    76  		return
    77  	}
    78  
    79  	w.WriteHeader(http.StatusOK)
    80  	w.Header().Set("Content-Type", "application/json")
    81  	buffer.WriteTo(w)
    82  }
    83  
    84  func Encode(w http.ResponseWriter, r *http.Request) {
    85  	msg, err := getMsgType(r)
    86  	if err != nil {
    87  		w.WriteHeader(http.StatusNotFound)
    88  		fmt.Fprintln(w, err)
    89  		return
    90  	}
    91  
    92  	err = protolator.DeepUnmarshalJSON(r.Body, msg)
    93  	if err != nil {
    94  		w.WriteHeader(http.StatusBadRequest)
    95  		fmt.Fprintln(w, err)
    96  		return
    97  	}
    98  
    99  	data, err := proto.Marshal(msg)
   100  	if err != nil {
   101  		w.WriteHeader(http.StatusBadRequest)
   102  		fmt.Fprintln(w, err)
   103  		return
   104  	}
   105  
   106  	w.WriteHeader(http.StatusOK)
   107  	w.Header().Set("Content-Type", "application/octet-stream")
   108  	w.Write(data)
   109  }