Example #1
0
func NewInventoryRequestResponder(st store.ObjectStore) *InventoryRequestResponder {
	x := &InventoryRequestResponder{APIResponder{path: RFPPATH, processorMap: make(map[string]RequestProcessor, 4)}}
	x.AddProcessor("POST", &InventoryRequestProcessor{StoreManager{store: st, pathKeys: util.Unmunge(RFPPATH)}})
	x.AddProcessor("GET", &GenericGetProcessor{StoreManager{store: st, pathKeys: util.Unmunge(RFPPATH)}})
	x.AddProcessor("DELETE", &GenericDeleteProcessor{StoreManager{store: st, pathKeys: util.Unmunge(RFPPATH)}})
	return x
}
Example #2
0
func NewCallbackReceiver(ts *testing.T) *CallbackReceiver {
	x := &CallbackReceiver{APIResponder: APIResponder{path: CB_PATH, processorMap: make(map[string]RequestProcessor, 4)}}
	cp := CallbackProcessor{t: ts}
	cp.pathKeys = util.Unmunge(CB_PATH)
	x.AddProcessor("POST", &cp)
	return x
}
Example #3
0
func (r APIResponder) Handle(req *http.Request) (int, []byte) {

	log.Println("APIResponder.Handle entered")
	method := req.Method
	u := req.URL
	pathTokens := util.Unmunge(u.Path)
	queryTokens := strings.Split(u.RawQuery, "&")

	// Read the request body
	buffer, err := r.ReadBody(req)
	if err != nil {
		return BAD, nil
	}
	log.Println("APIResponder.Handle: body read")

	pr := r.getProcessor(method)
	if &pr == nil {
		return BAD, nil
	}

	obj, err := pr.Unmarshal(buffer)
	if err != nil {
		return ERROR, nil
	}
	if obj != nil {
		log.Printf("Have object of type %T", obj)
		log.Printf("Received message with RequestId %s:\n%s\n", obj.GetKey(), buffer)
	} else {
		log.Println("Request did not include a body")
	}
	cerr := pr.ValidateRequest(pathTokens, queryTokens, obj)
	if cerr != nil {
		log.Printf("Invalid %s request: %s\n", method, cerr.Error())
		return cerr.Code(), nil
	}

	// Process it
	responses, cerr := pr.ProcessRequest(pathTokens, queryTokens, obj, &r)
	if cerr != nil {
		log.Printf("Error processing %s request: %s\n", method, cerr.Error())
		return cerr.Code(), nil
	}

	// Marshal the results into the response body
	if len(responses) > 0 {
		buffer, err = objects.Marshal(responses)
		log.Printf("Marshalling %d objects into response as \n%s", len(responses), buffer)
	}
	if err != nil {
		log.Printf("Error marshalling response: %s\n", err.Error())
		return ERROR, nil
	}
	log.Printf("%s Request handled", method)
	return OK, buffer

}
Example #4
0
func (s MapStore) EraseAll(keys []string) error {
	if len(keys) == 0 {
		return errors.New("No keys")
	}
	for key, _ := range s.store {
		toks := util.Unmunge(key)
		if s.isMatch(keys, toks) {
			s.Erase(toks)
		}
	}
	return nil
}
Example #5
0
func (n *Node) FindLeaf(path string) *Node {
	sp := util.Unmunge(path)
	cnt := len(sp)
	currNode := n
	for i := 1; i < cnt; i++ {
		nn := currNode.FindChild(sp[i])
		if nn == nil {
			return currNode
		}
		currNode = nn
	}
	return currNode
}
Example #6
0
func (s MapStore) GetAll(keys []string) ([]objects.Storable, error) {
	if len(keys) == 0 {
		return nil, errors.New("No keys")
	}
	ret := make([]objects.Storable, 0, 10)

	for key, value := range s.store {
		toks := util.Unmunge(key)
		if s.isMatch(keys, toks) {
			ret = append(ret, value)
		}
	}
	return ret, nil
}
Example #7
0
func TestSplit(t *testing.T) {
	val := "/Orders/Availability/"

	arr := util.Unmunge(val)
	t.Logf("Split %s into: ", val)
	for _, tok := range arr {
		if len(tok) > 0 {
			t.Log(tok)
		}
	}
	if len(arr) != 2 {
		t.Fatalf("Wrong number, got %d", len(arr))
	}
}
Example #8
0
func (n *Node) AddPath(path string) (*Node, error) {
	// Need to add code to strip any trailing /

	sp := util.Unmunge(path)
	cnt := len(sp)
	currNode := n
	for i := 1; i < cnt; i++ {
		nn := currNode.FindChild(sp[i])
		if nn == nil {
			log.Printf("Adding node %s to parent %s", sp[i], currNode.path)
			nn = newNode(sp[i])
			currNode.Add(nn)
		}
		currNode = nn
	}
	return currNode, nil
}