1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  *
26  */
27 
28 /* $Id: send-document.c 146 2006-03-24 00:26:54Z njacobs $ */
29 
30 #include <stdio.h>
31 #include <papi.h>
32 #include <ipp.h>
33 #include <ipp-listener.h>
34 
35 /*
36  * When the PAPI supports papiJobCreate(), papiJobStreamAdd() and
37  * papiJobClose(), this will be much cleaner and more efficient, but in the
38  * meantime, we are using a private, non-standard interface to do this.
39  */
40 papi_status_t
ipp_send_document(papi_service_t svc,papi_attribute_t ** request,papi_attribute_t *** response,ipp_reader_t iread,void * fd)41 ipp_send_document(papi_service_t svc, papi_attribute_t **request,
42 		papi_attribute_t ***response, ipp_reader_t iread, void *fd)
43 {
44 	papi_status_t status;
45 	papi_stream_t s = NULL;
46 	papi_job_t j = NULL;
47 	papi_attribute_t **operational = NULL;
48 	papi_attribute_t **job_attributes = NULL;
49 	char *queue = NULL;
50 	ssize_t rc;
51 	int id = -1;
52 	char buf[BUFSIZ];
53 	char last = PAPI_FALSE;
54 	char *keys[] = { "attributes-natural-language", "attributes-charset",
55 			"printer-uri", "job-id", "job-uri", "last-document",
56 			NULL };
57 
58 	/* Get operational attributes from the request */
59 	(void) papiAttributeListGetCollection(request, NULL,
60 				"operational-attributes-group", &operational);
61 
62 	/*
63 	 * the operational-attributes-group must contain:
64 	 *	job-uri (or printer-uri/job-id)
65 	 *	last-document
66 	 */
67 	get_printer_id(operational, &queue, &id);
68 	if (id < 0) {
69 		ipp_set_status(response, PAPI_BAD_REQUEST,
70 				"missing job-uri or job-id");
71 		return (PAPI_BAD_REQUEST);
72 	} else if (queue == NULL) {
73 		ipp_set_status(response, PAPI_BAD_REQUEST,
74 				"missing printer-uri or job-uri");
75 		return (PAPI_BAD_REQUEST);
76 	}
77 
78 	status = papiAttributeListGetBoolean(operational, NULL,
79 			"last-document", &last);
80 	if (status != PAPI_OK) {
81 		ipp_set_status(response, status, "last-document: %s",
82 				papiStatusString(status));
83 		return (PAPI_BAD_REQUEST);
84 	}
85 
86 	/*
87 	 * the operational-attributes-group may contain:
88 	 *	document-name
89 	 *	compression
90 	 *	document-format
91 	 *	document-natural-language
92 	 * Simply copy the entire contents of the operational-attributes-group
93 	 * for the PAPI call's possible use.
94 	 */
95 	split_and_copy_attributes(keys, operational, NULL, &job_attributes);
96 
97 	/* copy any job-attributes-group attributes for the PAPI call */
98 	if (papiAttributeListGetCollection(request, NULL,
99 			"job-attributes-group", &operational) == PAPI_OK)
100 		copy_attributes(&job_attributes, operational);
101 
102 	/* create a stream to write the document data on */
103 	status = papiJobStreamAdd(svc, queue, id, &s);
104 	papiAttributeListFree(job_attributes);
105 	if (status != PAPI_OK) {
106 		ipp_set_status(response, status, "job submission: %s",
107 				ipp_svc_status_mesg(svc, status));
108 		return (status);
109 	}
110 
111 	/* copy the document data from the IPP connection to the stream */
112 	while ((status == PAPI_OK) && ((rc = iread(fd, buf, sizeof (buf))) > 0))
113 		status = papiJobStreamWrite(svc, s, buf, rc);
114 	if (status != PAPI_OK) {
115 		ipp_set_status(response, status, "write job data: %s",
116 				ipp_svc_status_mesg(svc, status));
117 		return (status);
118 	}
119 
120 	/* close the stream */
121 	status = papiJobStreamClose(svc, s, &j);
122 	if (status != PAPI_OK) {
123 		ipp_set_status(response, status, "close job stream: %s",
124 				ipp_svc_status_mesg(svc, status));
125 		papiJobFree(j); /* we shouldn't have a job, but just in case */
126 		return (status);
127 	}
128 
129 	/* if it's the last document, commit the job */
130 	if (last == PAPI_TRUE) {
131 		status = papiJobCommit(svc, queue, id);
132 	}
133 
134 	/* add the job attributes to the response in a job-attributes-group */
135 	if (j != NULL) {
136 		papi_to_ipp_job_group(response, request, PAPI_ATTR_REPLACE, j);
137 		papiJobFree(j);
138 	}
139 
140 	return (status);
141 }
142