⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ipprequest.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      JobUri jobUri = (JobUri) attributes.get(JobUri.class);      JobId jobId = (JobId) attributes.get(JobId.class);      if (printerUri != null && jobId == null && jobUri == null)        {          write(printerUri);          attributes.remove(PrinterURI.class);          logger.log(Component.IPP, "Attribute: Name: <" + printerUri            .getCategory().getName() + "> Value: <" + printerUri.toString() + ">");        }      else if (jobUri != null && jobId == null && printerUri == null)        {          write(jobUri);          attributes.remove(JobUri.class);          logger.log(Component.IPP, "Attribute: Name: <" + jobUri            .getCategory().getName() + "> Value: <" + jobUri.toString() + ">");        }      else if (printerUri != null && jobId != null && jobUri == null)        {          write(printerUri); // must be third          write(jobId);          attributes.remove(PrinterURI.class);          attributes.remove(JobId.class);          logger.log(Component.IPP, "Attribute: Name: <" + printerUri            .getCategory().getName() + "> Value: <" + printerUri.toString() + ">");          logger.log(Component.IPP, "Attribute: Name: <" + jobId.getCategory()            .getName() + "> Value: <" + jobId.toString() + ">");        }      else if (jobUri != null && jobId != null)        {          write(jobUri);          attributes.remove(JobUri.class);          attributes.remove(JobId.class); // MUST NOT redundant          logger.log(Component.IPP, "Attribute: Name: <" + jobUri.getCategory()            .getName() + "> Value: <" + jobUri.toString() + ">");        }      else        {          new IppException("Unknown target operation attribute combination.");        }                  writeAttributes(attributes);    }        /**     * Writes the given attribute groups 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 writeAttributes(AttributeSet attributes)        throws IOException, IppException    {      Attribute[] attributeArray = attributes.toArray();      for (int i = 0; i < attributeArray.length; i++)        {          logger.log(Component.IPP, "Attribute: Name: <" + attributeArray[i]            .getCategory().getName() + "> Value: <"             + attributeArray[i].toString() + ">");                              if (attributeArray[i] instanceof IntegerSyntax)            write((IntegerSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof TextSyntax)            write((TextSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof DateTimeSyntax)            write((DateTimeSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof ResolutionSyntax)            write((ResolutionSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof SetOfIntegerSyntax)            write((SetOfIntegerSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof EnumSyntax)            write((EnumSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof URISyntax)            write((URISyntax) attributeArray[i]);          else if (attributeArray[i] instanceof CharsetSyntax)            write((CharsetSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof NaturalLanguageSyntax)            write((NaturalLanguageSyntax) attributeArray[i]);          else if (attributeArray[i] instanceof RequestedAttributes)            write((RequestedAttributes) attributeArray[i]);          else            throw new IppException("Unknown syntax type");        }    }  }  /**   * Logger for tracing - enable by passing   * -Dgnu.classpath.debug.components=ipp to the vm.   */  static final Logger logger = SystemLogger.SYSTEM;  /**   * The request id counter simply counts up   * to give unique request ids per JVM instance.   */  private static int requestIdCounter = 1;  /** The IPP version defaults to 1.1 */  private static final short VERSION = 0x0101;  /** Signals if the request is already on its way */  private boolean alreadySent = false;  /** The operation type of this request. */  private short operation_id;  /**    * The request id of this request. This is    * assigned automatically by the constructor.   */  private final int request_id;  private AttributeSet operationAttributes;  private AttributeSet printerAttributes;  private AttributeSet jobAttributes;  private Object data;    private URI requestUri;  /** The underlying connection - IPP is http based */  private HttpURLConnection  connection;    /**   * Creates an IPPRequest instance.   *    * @param uri the URI of the request   * @param user the user if any   * @param password the password of the supplied user   */  public IppRequest(URI uri, String user, String password)  {       request_id = incrementRequestIdCounter();    requestUri = uri;        try      {        URL url = new URL("http",                       user == null                       ? uri.getHost() : user + ":"                       + password + "@" + uri.getHost(),                       uri.getPort(), uri.getPath());               connection = (HttpURLConnection) url.openConnection();        connection.setRequestMethod("POST");        connection.setDoOutput(true);                connection.setRequestProperty("Content-type", "application/ipp");        connection.setRequestProperty("Accept", "text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2");              }       catch (IOException e)      {        // MalformedURLException - uri is already checked        // ProtocolException - POST is correct method type        // IOException -HTTPURLConnection constructor actually         // does never throw this exception.        logger.log(Component.IPP, "Unexpected IOException", e);      }        logger.log(Component.IPP, "[IppConnection] Host: " + uri.getHost()                              + " Port: " + uri.getPort() + " Path: "                              + uri.getPath());  }  /**   * Synchronized method to be called by the constructor   * to assign a unique request id to this request.   *    * @return The unique request id.   */  private synchronized int incrementRequestIdCounter()  {    return IppRequest.requestIdCounter++;  }  /**   * Returns the id of this request.   *    * @return The request ID.   */  public int getRequestID()  {    return request_id;  }  /**    * Sets the data of the request. The data used in this    * request will be the one of the supplied inputstream   * instead of the alternative byte array possibility.   *    * @param stream the input stream to use for the data.   */  public void setData(InputStream stream)  {    data = stream;  }  /**    * Sets the data of the request. The data used in this    * request will be the one of the supplied byte[]   * instead of the alternative input stream possibility.   *    * @param bytes the byte[] to use for the data.   */  public void setData(byte[] bytes)  {    data = bytes;  }  /**   * Sets the operation id for this request.   *    * @param id the operation id.   */  public void setOperationID(short id)  {    operation_id = id;  }  /**   * Adds the default values for the operation   * attributes "attributes-charset" and    * "attributes-natural-language"   */  public void setOperationAttributeDefaults()  {    if (operationAttributes == null)      operationAttributes = new HashAttributeSet();    operationAttributes.add(AttributesCharset.UTF8);    operationAttributes.add(AttributesNaturalLanguage.EN);  }    /**   * Add the job attribute of this request to the given   * attribute set.   *    * @param attribute the job attribute.   */  public void addJobAttribute(Attribute attribute)  {    if (jobAttributes == null)      jobAttributes = new HashAttributeSet();        jobAttributes.add(attribute);  }    /**   * Sets the printer attribute of this request to the given   * attribute set.   *    * @param attribute the printer attribute.   */  public void addPrinterAttributes(Attribute attribute)  {    if (printerAttributes == null)      printerAttributes = new HashAttributeSet();        printerAttributes.add(attribute);  }  /**   * Adds the given attribute to the operation attributes set.   *    * @param attribute the operation attribute to add.   */  public void addOperationAttribute(Attribute attribute)  {    if (operationAttributes == null)      operationAttributes = new HashAttributeSet();        operationAttributes.add(attribute);  }    /**   * Filters from the given attribute set the job operation out   * and adds them to the operation attributes set.   *    * @param set the attributes to filter, may not be <code>null</code>.   */  public void addAndFilterJobOperationAttributes(AttributeSet set)  {    if (operationAttributes == null)      operationAttributes = new HashAttributeSet();        // document-natural-language - not defined in JPS attributes    // document-format - specified outside, special treatment    Attribute[] tmp = set.toArray();    for (int i = 0; i < tmp.length; i++)       {                if (tmp[i].getCategory().equals(JobName.class)            || tmp[i].getCategory().equals(Fidelity.class)            || tmp[i].getCategory().equals(JobImpressions.class)            || tmp[i].getCategory().equals(JobKOctets.class)            || tmp[i].getCategory().equals(JobMediaSheets.class)            || tmp[i].getCategory().equals(Compression.class)            || tmp[i].getCategory().equals(DocumentName.class)            || tmp[i].getCategory().equals(RequestingUserName.class))                          operationAttributes.add(tmp[i]);                  }      }    /**   * Filters from the given attribute set the job template attributes   * out and adds them to the job attributes set.   *    * @param set the attributes to filter, may not be <code>null</code>.   */  public void addAndFilterJobTemplateAttributes(AttributeSet set)  {    if (jobAttributes == null)      jobAttributes = new HashAttributeSet();        // document-natural-language - not defined in JPS attributes    // document-format - specified outside, special treatment    Attribute[] tmp = set.toArray();    for (int i = 0; i < tmp.length; i++)       {                if (tmp[i].getCategory().equals(JobPriority.class)            || tmp[i].getCategory().equals(JobHoldUntil.class)            || tmp[i].getCategory().equals(JobSheets.class)            || tmp[i].getCategory().equals(MultipleDocumentHandling.class)            || tmp[i].getCategory().equals(Copies.class)            || tmp[i].getCategory().equals(Finishings.class)            || tmp[i].getCategory().equals(PageRanges.class)            || tmp[i].getCategory().equals(NumberUp.class)            || tmp[i].getCategory().equals(OrientationRequested.class)            || tmp[i].getCategory().equals(Media.class)            || tmp[i].getCategory().equals(PrinterResolution.class)            || tmp[i].getCategory().equals(PrintQuality.class)            || tmp[i].getCategory().equals(SheetCollate.class)            || tmp[i].getCategory().equals(Sides.class))                          jobAttributes.add(tmp[i]);                  }      }  /**   * Does some validation of the supplied parameters and then   * sends the request to the ipp server or service.   *    * @return The response if any.   *    * @throws IllegalStateException if request is already sent   * @throws IppException if connection or request failed.   * @throws IOException if writing of the header, attributes or footer fails.    */  public IppResponse send() throws IppException, IOException  {    if (alreadySent)      throw new IllegalStateException("Request is already sent");        alreadySent = true;          OutputStream stream = stream = connection.getOutputStream();       DataOutputStream out = new DataOutputStream(stream);            //  the header 8 bytes long    out.writeShort(VERSION);    out.writeShort(operation_id);    out.writeInt(request_id);            logger.log(Component.IPP, "OperationID: " + Integer.toHexString(operation_id)       + " RequestID: " + request_id);             // Pass stuff the the attribute writer which knows how to    // write the attributes in correct order    logger.log(Component.IPP, "Operation Attributes");            RequestWriter writer = new RequestWriter(out);    writer.writeOperationAttributes(operationAttributes);                   if (jobAttributes != null)      {        logger.log(Component.IPP, "Job Attributes");        out.write(IppDelimiterTag.JOB_ATTRIBUTES_TAG);        writer.writeAttributes(jobAttributes);      }               if (printerAttributes != null)      {        logger.log(Component.IPP, "Printer Attributes");        out.write(IppDelimiterTag.PRINTER_ATTRIBUTES_TAG);        writer.writeAttributes(printerAttributes);      }              // write the delimiter to the data    out.write(IppDelimiterTag.END_OF_ATTRIBUTES_TAG);                   // check if data is byte[] or inputstream    if (data instanceof InputStream)      {        byte[] readbuf = new byte[2048];        int len = 0;                    while( (len = ((InputStream) data).read(readbuf)) > 0)          out.write(readbuf, 0, len);      }    else if (data != null)      {        out.write((byte[]) data);      }                 out.flush();    stream.flush();          int responseCode = responseCode = connection.getResponseCode();        if (responseCode == HttpURLConnection.HTTP_OK)      {         IppResponse response = new IppResponse(requestUri, operation_id);                response.setResponseData(connection.getInputStream());             return response;      }    logger.log(Component.IPP, "HTTP-Statuscode: " + responseCode);    throw new IppException("Request failed got HTTP status code "                            + responseCode);  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -