func (server *Server) serveGraph(writer http.ResponseWriter, request *http.Request) { graphID := strings.TrimPrefix(request.URL.Path, urlLibraryPath+"graphs/") switch request.Method { case "DELETE": if graphID == "" { server.serveResponse(writer, serverResponse{mesgMethodNotAllowed}, http.StatusMethodNotAllowed) return } err := server.Library.DeleteItem(graphID, library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } server.serveResponse(writer, nil, http.StatusOK) case "GET", "HEAD": if graphID == "" { server.serveGraphList(writer, request) return } item, err := server.Library.GetItem(graphID, library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } server.serveResponse(writer, item, http.StatusOK) case "POST", "PUT": var graph *library.Graph if response, status := server.parseStoreRequest(writer, request, graphID); status != http.StatusOK { server.serveResponse(writer, response, status) return } if request.Method == "POST" && request.FormValue("inherit") != "" { // Get graph from library item, err := server.Library.GetItem(request.FormValue("inherit"), library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } graph = &library.Graph{} utils.Clone(item.(*library.Graph), graph) graph.ID = "" } else { // Create a new graph instance graph = &library.Graph{Item: library.Item{ID: graphID}} } graph.Modified = time.Now() // Parse input JSON for graph data body, _ := ioutil.ReadAll(request.Body) if err := json.Unmarshal(body, graph); err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgResourceInvalid}, http.StatusBadRequest) return } err := server.Library.StoreItem(graph, library.LibraryItemGraph) if response, status := server.parseError(writer, request, err); status != http.StatusOK { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, response, status) return } if request.Method == "POST" { writer.Header().Add("Location", strings.TrimRight(request.URL.Path, "/")+"/"+graph.ID) server.serveResponse(writer, nil, http.StatusCreated) } else { server.serveResponse(writer, nil, http.StatusOK) } default: server.serveResponse(writer, serverResponse{mesgMethodNotAllowed}, http.StatusMethodNotAllowed) } }
func (server *Server) serveGraph(writer http.ResponseWriter, request *http.Request) { var graph *library.Graph if request.Method != "GET" && request.Method != "HEAD" && server.Config.ReadOnly { server.serveResponse(writer, serverResponse{mesgReadOnlyMode}, http.StatusForbidden) return } graphID := routeTrimPrefix(request.URL.Path, urlLibraryPath+"graphs") switch request.Method { case "DELETE": if graphID == "" { server.serveResponse(writer, serverResponse{mesgMethodNotAllowed}, http.StatusMethodNotAllowed) return } err := server.Library.DeleteItem(graphID, library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } server.serveResponse(writer, nil, http.StatusOK) case "GET", "HEAD": if graphID == "" { server.serveGraphList(writer, request) return } item, err := server.Library.GetItem(graphID, library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } server.serveResponse(writer, item, http.StatusOK) case "POST", "PUT": if response, status := server.parseStoreRequest(writer, request, graphID); status != http.StatusOK { server.serveResponse(writer, response, status) return } // Inheritance requested: clone an existing graph if request.Method == "POST" && request.FormValue("inherit") != "" { item, err := server.Library.GetItem(request.FormValue("inherit"), library.LibraryItemGraph) if os.IsNotExist(err) { server.serveResponse(writer, serverResponse{mesgResourceNotFound}, http.StatusNotFound) return } else if err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgUnhandledError}, http.StatusInternalServerError) return } // Clone item graph = &library.Graph{} utils.Clone(item.(*library.Graph), graph) // Reset item identifier graph.ID = "" } else { // Create a new graph instance graph = &library.Graph{Item: library.Item{ID: graphID}} } // Parse input JSON for graph data body, _ := ioutil.ReadAll(request.Body) if err := json.Unmarshal(body, graph); err != nil { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, serverResponse{mesgResourceInvalid}, http.StatusBadRequest) return } // Check for graph type consistency (either a graph or an instance but not both) if !graph.Template && (graph.Link != "" || len(graph.Attributes) > 0) && (graph.Description != "" || graph.Title != "" || graph.Type != 0 || graph.StackMode != 0 || graph.UnitType != 0 || graph.UnitLegend != "" || graph.Groups != nil) || graph.Template && (graph.Link != "" || len(graph.Attributes) > 0) { server.serveResponse(writer, serverResponse{mesgResourceInvalid}, http.StatusBadRequest) return } // Store graph item err := server.Library.StoreItem(graph, library.LibraryItemGraph) if response, status := server.parseError(writer, request, err); status != http.StatusOK { logger.Log(logger.LevelError, "server", "%s", err) server.serveResponse(writer, response, status) return } if request.Method == "POST" { writer.Header().Add("Location", strings.TrimRight(request.URL.Path, "/")+"/"+graph.ID) server.serveResponse(writer, nil, http.StatusCreated) } else { server.serveResponse(writer, nil, http.StatusOK) } default: server.serveResponse(writer, serverResponse{mesgMethodNotAllowed}, http.StatusMethodNotAllowed) } }