func negotiateOutputSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.Serializer, string, error) { acceptHeader := req.Header.Get("Accept") supported := ns.SupportedMediaTypes() if len(acceptHeader) == 0 && len(supported) > 0 { acceptHeader = supported[0] } accept, ok := negotiate(acceptHeader, supported) if !ok { return nil, "", errNotAcceptable{supported} } pretty := isPrettyPrint(req) if _, ok := accept.Params["pretty"]; !ok && pretty { accept.Params["pretty"] = "1" } mediaType := accept.Type if len(accept.SubType) > 0 { mediaType += "/" + accept.SubType } if s, ok := ns.SerializerForMediaType(mediaType, accept.Params); ok { return s, mediaType, nil } return nil, "", errNotAcceptable{supported} }
// Adds a service to return the supported resources, E.g., a such web service // will be registered at /apis/extensions/v1. func AddSupportedResourcesWebService(s runtime.NegotiatedSerializer, ws *restful.WebService, groupVersion unversioned.GroupVersion, apiResources []unversioned.APIResource) { resourceHandler := SupportedResourcesHandler(s, groupVersion, apiResources) ws.Route(ws.GET("/").To(resourceHandler). Doc("get available resources"). Operation("getAPIResources"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) }
// mediaTypesForSerializer returns a list of media and stream media types for the server. func mediaTypesForSerializer(ns runtime.NegotiatedSerializer) (mediaTypes, streamMediaTypes []string) { for _, info := range ns.SupportedMediaTypes() { mediaTypes = append(mediaTypes, info.MediaType) if info.StreamSerializer != nil { // stream=watch is the existing mime-type parameter for watch streamMediaTypes = append(streamMediaTypes, info.MediaType+";stream=watch") } } return mediaTypes, streamMediaTypes }
func negotiateOutputStreamSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.StreamSerializerInfo, error) { supported := ns.SupportedMediaTypes() mediaType, params, err := negotiateOutput(req, supported) if err != nil { return runtime.StreamSerializerInfo{}, err } if s, ok := ns.StreamingSerializerForMediaType(mediaType, params); ok { return s, nil } return runtime.StreamSerializerInfo{}, errNotAcceptable{supported} }
func negotiateOutputStreamSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.Serializer, runtime.Framer, string, string, error) { supported := ns.SupportedMediaTypes() mediaType, params, err := negotiateOutput(req, supported) if err != nil { return nil, nil, "", "", err } if s, f, exactMediaType, ok := ns.StreamingSerializerForMediaType(mediaType, params); ok { return s, f, mediaType, exactMediaType, nil } return nil, nil, "", "", errNotAcceptable{supported} }
// Adds a service to return the supported versions, preferred version, and name // of a group. E.g., a such web service will be registered at /apis/extensions. func AddGroupWebService(s runtime.NegotiatedSerializer, container *restful.Container, path string, group unversioned.APIGroup) { groupHandler := GroupHandler(s, group) ws := new(restful.WebService) ws.Path(path) ws.Doc("get information of a group") ws.Route(ws.GET("/").To(groupHandler). Doc("get information of a group"). Operation("getAPIGroup"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
// Adds a service to return the supported api versions at /apis. func AddApisWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, f func() []unversioned.APIGroup) { rootAPIHandler := RootAPIHandler(s, f) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(rootAPIHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
// Adds a service to return the supported api versions at the legacy /api. func AddApiWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, versions []string) { // TODO: InstallREST should register each version automatically versionHandler := APIVersionHandler(s, versions[:]...) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(versionHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
// Adds a service to return the supported resources, E.g., a such web service // will be registered at /apis/extensions/v1. func AddSupportedResourcesWebService(s runtime.NegotiatedSerializer, ws *restful.WebService, groupVersion unversioned.GroupVersion, apiResources []unversioned.APIResource) { ss := s if keepUnversioned(groupVersion.Group) { // Because in release 1.1, /apis/extensions/v1beta1 returns response // with empty APIVersion, we use StripVersionNegotiatedSerializer to // keep the response backwards compatible. ss = StripVersionNegotiatedSerializer{s} } resourceHandler := SupportedResourcesHandler(ss, groupVersion, apiResources) ws.Route(ws.GET("/").To(resourceHandler). Doc("get available resources"). Operation("getAPIResources"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) }
func negotiateInputSerializer(req *http.Request, s runtime.NegotiatedSerializer) (runtime.SerializerInfo, error) { supported := s.SupportedMediaTypes() mediaType := req.Header.Get("Content-Type") if len(mediaType) == 0 { mediaType = supported[0] } mediaType, options, err := mime.ParseMediaType(mediaType) if err != nil { return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} } out, ok := s.SerializerForMediaType(mediaType, options) if !ok { return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} } return out, nil }
// acceptedMediaTypesForEndpoint returns an array of structs that are used to efficiently check which // allowed media types the server exposes. func acceptedMediaTypesForEndpoint(ns runtime.NegotiatedSerializer) []acceptedMediaType { var acceptedMediaTypes []acceptedMediaType for _, info := range ns.SupportedMediaTypes() { segments := strings.SplitN(info.MediaType, "/", 2) if len(segments) == 1 { segments = append(segments, "*") } t := acceptedMediaType{ Type: segments[0], SubType: segments[1], Serializer: info, } acceptedMediaTypes = append(acceptedMediaTypes, t) } return acceptedMediaTypes }
// Adds a service to return the supported api versions at /apis. func AddApisWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, f func(req *restful.Request) []unversioned.APIGroup) { // Because in release 1.1, /apis returns response with empty APIVersion, we // use StripVersionNegotiatedSerializer to keep the response backwards // compatible. ss := StripVersionNegotiatedSerializer{s} rootAPIHandler := RootAPIHandler(ss, f) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(rootAPIHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
// Adds a service to return the supported api versions at the legacy /api. func AddApiWebService(s runtime.NegotiatedSerializer, container *restful.Container, apiPrefix string, getAPIVersionsFunc func(req *restful.Request) *unversioned.APIVersions) { // TODO: InstallREST should register each version automatically // Because in release 1.1, /api returns response with empty APIVersion, we // use StripVersionNegotiatedSerializer to keep the response backwards // compatible. ss := StripVersionNegotiatedSerializer{s} versionHandler := APIVersionHandler(ss, getAPIVersionsFunc) ws := new(restful.WebService) ws.Path(apiPrefix) ws.Doc("get available API versions") ws.Route(ws.GET("/").To(versionHandler). Doc("get available API versions"). Operation("getAPIVersions"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
// Adds a service to return the supported versions, preferred version, and name // of a group. E.g., a such web service will be registered at /apis/extensions. func AddGroupWebService(s runtime.NegotiatedSerializer, container *restful.Container, path string, group unversioned.APIGroup) { ss := s if keepUnversioned(group.Name) { // Because in release 1.1, /apis/extensions returns response with empty // APIVersion, we use StripVersionNegotiatedSerializer to keep the // response backwards compatible. ss = StripVersionNegotiatedSerializer{s} } groupHandler := GroupHandler(ss, group) ws := new(restful.WebService) ws.Path(path) ws.Doc("get information of a group") ws.Route(ws.GET("/").To(groupHandler). Doc("get information of a group"). Operation("getAPIGroup"). Produces(s.SupportedMediaTypes()...). Consumes(s.SupportedMediaTypes()...)) container.Add(ws) }
func negotiateInputSerializer(req *http.Request, ns runtime.NegotiatedSerializer) (runtime.SerializerInfo, error) { mediaTypes := ns.SupportedMediaTypes() mediaType := req.Header.Get("Content-Type") if len(mediaType) == 0 { mediaType = mediaTypes[0].MediaType } mediaType, _, err := mime.ParseMediaType(mediaType) if err != nil { _, supported := mediaTypesForSerializer(ns) return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} } for _, info := range mediaTypes { if info.MediaType != mediaType { continue } return info, nil } _, supported := mediaTypesForSerializer(ns) return runtime.SerializerInfo{}, errUnsupportedMediaType{supported} }