📄 ipprequest.java
字号:
/* IppRequest.java -- Copyright (C) 2006 Free Software Foundation, Inc. This file is part of GNU Classpath. GNU Classpath is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. GNU Classpath is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with GNU Classpath; see the file COPYING. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. Linking this library statically or dynamically with other modules is making a combined work based on this library. Thus, the terms and conditions of the GNU General Public License cover the whole combination. As a special exception, the copyright holders of this library give you permission to link this library with independent modules to produce an executable, regardless of the license terms of these independent modules, and to copy and distribute the resulting executable under terms of your choice, provided that you also meet, for each linked independent module, the terms and conditions of the license of that module. An independent module is a module which is not derived from or based on this library. If you modify this library, you may extend this exception to your version of the library, but you are not obligated to do so. If you do not wish to do so, delete this exception statement from your version. */package gnu.javax.print.ipp;import gnu.classpath.debug.Component;import gnu.classpath.debug.SystemLogger;import gnu.javax.print.ipp.attribute.CharsetSyntax;import gnu.javax.print.ipp.attribute.NaturalLanguageSyntax;import gnu.javax.print.ipp.attribute.RequestedAttributes;import gnu.javax.print.ipp.attribute.job.AttributesCharset;import gnu.javax.print.ipp.attribute.job.AttributesNaturalLanguage;import gnu.javax.print.ipp.attribute.job.JobId;import gnu.javax.print.ipp.attribute.job.JobUri;import gnu.javax.print.ipp.attribute.printer.DocumentFormat;import java.io.DataOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URI;import java.net.URL;import java.util.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.List;import java.util.logging.Logger;import javax.print.attribute.Attribute;import javax.print.attribute.AttributeSet;import javax.print.attribute.DateTimeSyntax;import javax.print.attribute.EnumSyntax;import javax.print.attribute.HashAttributeSet;import javax.print.attribute.IntegerSyntax;import javax.print.attribute.ResolutionSyntax;import javax.print.attribute.SetOfIntegerSyntax;import javax.print.attribute.TextSyntax;import javax.print.attribute.URISyntax;import javax.print.attribute.standard.Compression;import javax.print.attribute.standard.Copies;import javax.print.attribute.standard.DocumentName;import javax.print.attribute.standard.Fidelity;import javax.print.attribute.standard.Finishings;import javax.print.attribute.standard.JobHoldUntil;import javax.print.attribute.standard.JobImpressions;import javax.print.attribute.standard.JobKOctets;import javax.print.attribute.standard.JobMediaSheets;import javax.print.attribute.standard.JobName;import javax.print.attribute.standard.JobOriginatingUserName;import javax.print.attribute.standard.JobPriority;import javax.print.attribute.standard.JobSheets;import javax.print.attribute.standard.Media;import javax.print.attribute.standard.MultipleDocumentHandling;import javax.print.attribute.standard.NumberUp;import javax.print.attribute.standard.OrientationRequested;import javax.print.attribute.standard.PageRanges;import javax.print.attribute.standard.PrintQuality;import javax.print.attribute.standard.PrinterResolution;import javax.print.attribute.standard.PrinterURI;import javax.print.attribute.standard.RequestingUserName;import javax.print.attribute.standard.SheetCollate;import javax.print.attribute.standard.Sides;/** * <code>IppRequest</code> models a request to an IPP compatible * server as described in RFC 2910 - IPP/1.1: Encoding and Transport. * <p> * The byte stream is structured as follows (for an official description * please have a look at the RFC document mentioned above): * <ul> * <li>version-number - 2 bytes - required</li> * <li>operation-id - 2 bytes - required</li> * <li>request-id - 4 bytes - required</li> * <li>attribute-group - n bytes - 0 or more</li> * <li>end-of-attributes-tag - 1 byte - required</li> * <li>data - q bytes - optional</li> * </ul> * </p> * * @author Wolfgang Baer (WBaer@gmx.de) */public class IppRequest{ /** * Helper class used to write the attributes of a request * into the supplied data output stream in the correct way. * * @author Wolfgang Baer (WBaer@gmx.de) */ class RequestWriter { private DataOutputStream out; /** * Creates a RequestWriter. * * @param stream the stream to write to. */ RequestWriter(DataOutputStream stream) { out = stream; } /** * Writes an attribute in IntegerSyntax into the stream. * @param attribute the attribute * @throws IOException if thrown by the stream */ private void write(IntegerSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.INTEGER); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(4); // length, integer is 4 bytes out.writeInt(attribute.getValue()); } /** * Writes an attribute in EnumSyntax into the stream. * @param attribute the attribute * @throws IOException if thrown by the stream */ private void write(EnumSyntax attribute) throws IOException { // in JPS API enum syntax is used for enums, keyword and boolean types String name = ((Attribute) attribute).getName(); // the enum value types if (attribute instanceof Finishings || attribute instanceof OrientationRequested || attribute instanceof PrintQuality) { out.writeByte(IppValueTag.ENUM); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(4); // length, enum is 4 bytes out.writeInt(attribute.getValue()); } // the boolean value type else if (attribute instanceof Fidelity) { out.writeByte(IppValueTag.BOOLEAN); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(1); // length, boolean is 1 bytes out.writeByte(attribute.getValue() == 0 ? 0x00 : 0x01); } // the keyword value types else { String keyword = attribute.toString(); out.writeByte(IppValueTag.KEYWORD); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(keyword.length()); out.write(keyword.getBytes()); } } /** * Writes an attribute in SetOfIntegerSyntax into the stream. * @param attribute the attribute * @throws IOException if thrown by the stream */ private void write(SetOfIntegerSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); int[][] ranges = attribute.getMembers(); for (int i = 0; i < ranges.length; i++) { out.writeByte(IppValueTag.RANGEOFINTEGER); if (i == 0) { out.writeShort(name.length()); out.write(name.getBytes()); } else out.writeShort(0x0000); // only name-length out.writeShort(8); // range is 8 bytes out.writeInt(ranges[i][0]); out.writeInt(ranges[i][1]); } } /** * Writes an attribute in ResolutionSyntax into the stream. * @param attribute the attribute * @throws IOException if thrown by the stream */ private void write(ResolutionSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.RESOLUTION); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(9); // length fixed to 9 out.writeInt(attribute.getCrossFeedResolution(ResolutionSyntax.DPI)); out.writeInt(attribute.getFeedResolution(ResolutionSyntax.DPI)); out.writeByte(ResolutionSyntax.DPI); } /** * Writes an attribute in DateTimeSyntax into the stream. * <p> * The syntax value is defined as 11 octets follwing the * DateAndTime format of RFC 1903. (see IppResponse) * </p> * * @param attribute the attribute * @throws IOException if thrown by the stream */ private void write(DateTimeSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.DATETIME); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(11); // length fixed to 11 Date date = attribute.getValue(); Calendar cal = new GregorianCalendar(); cal.setTime(date); out.writeShort(cal.get(Calendar.YEAR)); out.writeByte(cal.get(Calendar.MONTH)); out.writeByte(cal.get(Calendar.DAY_OF_MONTH)); out.writeByte(cal.get(Calendar.HOUR_OF_DAY)); out.writeByte(cal.get(Calendar.MINUTE)); int second = cal.get(Calendar.SECOND); out.writeByte(second == 0 ? 60 : second); out.writeByte(cal.get(Calendar.MILLISECOND) / 100); int offsetInMillis = cal.get(Calendar.ZONE_OFFSET); char directionFromUTC = '+'; if (offsetInMillis < 0) { directionFromUTC = '-'; offsetInMillis = offsetInMillis * (-1); } out.writeByte(directionFromUTC); out.writeByte(offsetInMillis / 3600000); // hours out.writeByte((offsetInMillis % 3600000) / 60000); // minutes } /** * Writes an attribute in TextSyntax into the stream. * <p> * By default attributes are qritten as TEXT_WITHOUT_LANGUAGE value-tag. * As some attributes in the JPS are TextSyntax attributes but actually * of NAME value-tag in IPP this method checks for these attributes and * writes them as NAME_WITHOUT_LANGUAGE value-tag into the stream. * </p> * * @param attribute the attribute * @param out the stream to write to * @throws IOException if thrown by the stream */ private void write(TextSyntax attribute) throws IOException { // We only use *WithoutLanguage, correct according to spec. String name = ((Attribute) attribute).getName(); if (attribute instanceof RequestingUserName || attribute instanceof JobName || attribute instanceof DocumentName || attribute instanceof JobOriginatingUserName) out.writeByte(IppValueTag.NAME_WITHOUT_LANGUAGE); else if (attribute instanceof DocumentFormat) out.writeByte(IppValueTag.MIME_MEDIA_TYPE); else out.writeByte(IppValueTag.TEXT_WITHOUT_LANGUAGE); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(attribute.getValue().length()); out.write(attribute.getValue().getBytes()); } /** * Writes an attribute in URISyntax into the stream. * @param attribute the attribute * @param out the stream to write to * @throws IOException if thrown by the stream */ private void write(URISyntax attribute) throws IOException { // only uriScheme syntax type should not appear // in a request (reference-uri-schemes-supported) String name = ((Attribute) attribute).getName(); String uriAscii = attribute.getURI().toASCIIString(); out.writeByte(IppValueTag.URI); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(uriAscii.length()); out.write(uriAscii.getBytes()); } /** * Writes an attribute in CharsetSyntax into the stream. * @param attribute the attribute * @param out the stream to write to * @throws IOException if thrown by the stream */ private void write(CharsetSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.CHARSET); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(attribute.getValue().length()); out.write(attribute.getValue().getBytes()); } /** * Writes an attribute in NaturalLanguageSyntax into the stream. * @param attribute the attribute * @param out the stream to write to * @throws IOException if thrown by the stream */ private void write(NaturalLanguageSyntax attribute) throws IOException { String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.NATURAL_LANGUAGE); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(attribute.getValue().length()); out.write(attribute.getValue().getBytes()); } /** * Writes an attribute in RequestedAttributes into the stream. * @param attribute the attribute * @param out the stream to write to * @throws IOException if thrown by the stream */ private void write(RequestedAttributes attribute) throws IOException { List values = attribute.getValues(); String name = ((Attribute) attribute).getName(); out.writeByte(IppValueTag.KEYWORD); out.writeShort(name.length()); out.write(name.getBytes()); out.writeShort(((String) values.get(0)).length()); out.write(((String) values.get(0)).getBytes()); for (int i=1; i < values.size(); i++) { out.writeByte(IppValueTag.KEYWORD); out.writeShort(0x0000); // length for additional value out.writeShort(((String) values.get(i)).length()); out.write(((String) values.get(i)).getBytes()); } } /** * Writes the given operation attribute group of the given map instance * (key=group, values=set of attributes) into the supplied data * output stream. * * @param attributes the set with the attributes. * * @throws IOException if thrown by the used DataOutputStream. * @throws IppException if unknown attributes occur. */ public void writeOperationAttributes(AttributeSet attributes) throws IOException, IppException { out.write(IppDelimiterTag.OPERATION_ATTRIBUTES_TAG); // its essential to write these two in this order and as first ones Attribute att = attributes.get(AttributesCharset.class); write((CharsetSyntax) att); logger.log(Component.IPP, "Attribute: Name: <" + att.getCategory().getName() + "> Value: <" + att.toString() + ">"); attributes.remove(AttributesCharset.class); att = attributes.get(AttributesNaturalLanguage.class); write((NaturalLanguageSyntax) att); attributes.remove(AttributesNaturalLanguage.class); logger.log(Component.IPP, "Attribute: Name: <" + att.getCategory().getName() + "> Value: <" + att.toString() + ">"); // furthermore its essential to now write out the target attribute PrinterURI printerUri = (PrinterURI) attributes.get(PrinterURI.class);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -