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