diff --git a/adapter-socket.go b/adapter-socket.go index a5806a8..a0faee7 100644 --- a/adapter-socket.go +++ b/adapter-socket.go @@ -9,7 +9,6 @@ import ( "net" "net/http" "os" - "os/user" "strconv" ) @@ -43,13 +42,6 @@ func NewSocketAdapter(host string, useTLS bool) *SocketAdapter { //DoRequest performs the given IPP request to the given URL, returning the IPP response or an error if one occurred func (h *SocketAdapter) SendRequest(url string, r *Request, _ io.Writer) (*Response, error) { - // set user field - user, err := user.Current() - if err != nil { - return nil, fmt.Errorf("unable to lookup current user: %v", err) - } - r.OperationAttributes[AttributeRequestingUserName] = user.Username - for i := 0; i < h.requestRetryLimit; i++ { // encode request payload, err := r.Encode() diff --git a/cups-client.go b/cups-client.go index 322bd64..54c4589 100644 --- a/cups-client.go +++ b/cups-client.go @@ -26,7 +26,7 @@ func NewCUPSClientWithAdapter(username string, adapter Adapter) *CUPSClient { func (c *CUPSClient) GetDevices() (map[string]Attributes, error) { req := NewRequest(OperationCupsGetDevices, 1) - resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil) if err != nil { return nil, err } @@ -46,7 +46,7 @@ func (c *CUPSClient) MoveJob(jobID int, destPrinter string) error { req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter) - _, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil) return err } @@ -56,7 +56,7 @@ func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(srcPrinter) req.PrinterAttributes[AttributeJobPrinterURI] = c.getPrinterUri(destPrinter) - _, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil) return err } @@ -64,7 +64,7 @@ func (c *CUPSClient) MoveAllJob(srcPrinter, destPrinter string) error { func (c *CUPSClient) GetPPDs() (map[string]Attributes, error) { req := NewRequest(OperationCupsGetPPDs, 1) - resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil) if err != nil { return nil, err } @@ -83,7 +83,7 @@ func (c *CUPSClient) AcceptJobs(printer string) error { req := NewRequest(OperationCupsAcceptJobs, 1) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -92,7 +92,7 @@ func (c *CUPSClient) RejectJobs(printer string) error { req := NewRequest(OperationCupsRejectJobs, 1) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -124,7 +124,7 @@ func (c *CUPSClient) AddPrinterToClass(class, printer string) error { req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) req.PrinterAttributes[AttributeMemberURIs] = memberURIList - _, err = c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err = c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -154,7 +154,7 @@ func (c *CUPSClient) DeletePrinterFromClass(class, printer string) error { req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) req.PrinterAttributes[AttributeMemberURIs] = memberURIList - _, err = c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err = c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -163,7 +163,7 @@ func (c *CUPSClient) DeleteClass(class string) error { req := NewRequest(OperationCupsDeleteClass, 1) req.OperationAttributes[AttributePrinterURI] = c.getClassUri(class) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -179,7 +179,7 @@ func (c *CUPSClient) CreatePrinter(name, deviceURI, ppd string, shared bool, err req.PrinterAttributes[AttributePrinterLocation] = location req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -189,7 +189,7 @@ func (c *CUPSClient) SetPrinterPPD(printer, ppd string) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePPDName] = ppd - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -199,7 +199,7 @@ func (c *CUPSClient) SetPrinterDeviceURI(printer, deviceURI string) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.PrinterAttributes[AttributeDeviceURI] = deviceURI - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -209,7 +209,7 @@ func (c *CUPSClient) SetPrinterIsShared(printer string, shared bool) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePrinterIsShared] = shared - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -219,7 +219,7 @@ func (c *CUPSClient) SetPrinterErrorPolicy(printer string, errorPolicy string) e req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.PrinterAttributes[AttributePrinterErrorPolicy] = string(errorPolicy) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -229,7 +229,7 @@ func (c *CUPSClient) SetPrinterInformation(printer, information string) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.PrinterAttributes[AttributePrinterInfo] = information - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -239,7 +239,7 @@ func (c *CUPSClient) SetPrinterLocation(printer, location string) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.PrinterAttributes[AttributePrinterLocation] = location - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -248,7 +248,7 @@ func (c *CUPSClient) DeletePrinter(printer string) error { req := NewRequest(OperationCupsDeletePrinter, 1) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -262,7 +262,7 @@ func (c *CUPSClient) GetPrinters(attributes []string) (map[string]Attributes, er req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName) } - resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil) if err != nil { return nil, err } @@ -286,7 +286,7 @@ func (c *CUPSClient) GetClasses(attributes []string) (map[string]Attributes, err req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributePrinterName) } - resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil) if err != nil { return nil, err } diff --git a/ipp-client.go b/ipp-client.go index 76bb27a..0a992a8 100644 --- a/ipp-client.go +++ b/ipp-client.go @@ -40,10 +40,6 @@ func NewIPPClientWithAdapter(username string, adapter Adapter) *IPPClient { } } -func (c *IPPClient) getHttpUri(namespace string, object interface{}) string { - return c.adapter.GetHttpUri(namespace, object) -} - func (c *IPPClient) getPrinterUri(printer string) string { return fmt.Sprintf("ipp://localhost/printers/%s", printer) } @@ -58,6 +54,10 @@ func (c *IPPClient) getClassUri(printer string) string { // SendRequest sends a request to a remote uri end returns the response func (c *IPPClient) SendRequest(url string, req *Request, additionalResponseData io.Writer) (*Response, error) { + if _, ok := req.OperationAttributes[AttributeRequestingUserName]; ok == false { + req.OperationAttributes[AttributeRequestingUserName] = c.username + } + return c.adapter.SendRequest(url, req, additionalResponseData) } @@ -78,7 +78,7 @@ func (c *IPPClient) PrintDocuments(docs []Document, printer string, jobAttribute req.JobAttributes[key] = value } - resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil) if err != nil { return -1, err } @@ -102,7 +102,7 @@ func (c *IPPClient) PrintDocuments(docs []Document, printer string, jobAttribute req.File = doc.Document req.FileSize = doc.Size - _, err = c.SendRequest(c.getHttpUri("printers", printer), req, nil) + _, err = c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil) if err != nil { return -1, err } @@ -132,7 +132,7 @@ func (c *IPPClient) PrintJob(doc Document, printer string, jobAttributes map[str req.File = doc.Document req.FileSize = doc.Size - resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil) if err != nil { return -1, err } @@ -185,7 +185,7 @@ func (c *IPPClient) GetPrinterAttributes(printer string, attributes []string) (A req.OperationAttributes[AttributeRequestedAttributes] = attributes } - resp, err := c.SendRequest(c.getHttpUri("printers", printer), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("printers", printer), req, nil) if err != nil { return nil, err } @@ -202,7 +202,7 @@ func (c *IPPClient) ResumePrinter(printer string) error { req := NewRequest(OperationResumePrinter, 1) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -211,7 +211,7 @@ func (c *IPPClient) PausePrinter(printer string) error { req := NewRequest(OperationPausePrinter, 1) req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -226,7 +226,7 @@ func (c *IPPClient) GetJobAttributes(jobID int, attributes []string) (Attributes req.OperationAttributes[AttributeRequestedAttributes] = attributes } - resp, err := c.SendRequest(c.getHttpUri("jobs", jobID), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("jobs", jobID), req, nil) if err != nil { return nil, err } @@ -270,7 +270,7 @@ func (c *IPPClient) GetJobs(printer, class string, whichJobs string, myJobs bool req.OperationAttributes[AttributeRequestedAttributes] = append(attributes, AttributeJobID) } - resp, err := c.SendRequest(c.getHttpUri("", nil), req, nil) + resp, err := c.SendRequest(c.adapter.GetHttpUri("", nil), req, nil) if err != nil { return nil, err } @@ -290,7 +290,7 @@ func (c *IPPClient) CancelJob(jobID int, purge bool) error { req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.OperationAttributes[AttributePurgeJobs] = purge - _, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil) return err } @@ -300,7 +300,7 @@ func (c *IPPClient) CancelAllJob(printer string, purge bool) error { req.OperationAttributes[AttributePrinterURI] = c.getPrinterUri(printer) req.OperationAttributes[AttributePurgeJobs] = purge - _, err := c.SendRequest(c.getHttpUri("admin", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("admin", ""), req, nil) return err } @@ -309,7 +309,7 @@ func (c *IPPClient) RestartJob(jobID int) error { req := NewRequest(OperationRestartJob, 1) req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) - _, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil) return err } @@ -319,7 +319,7 @@ func (c *IPPClient) HoldJobUntil(jobID int, holdUntil string) error { req.OperationAttributes[AttributeJobURI] = c.getJobUri(jobID) req.JobAttributes[AttributeHoldJobUntil] = holdUntil - _, err := c.SendRequest(c.getHttpUri("jobs", ""), req, nil) + _, err := c.SendRequest(c.adapter.GetHttpUri("jobs", ""), req, nil) return err }