< prev index next >

src/java.desktop/unix/classes/sun/print/IPPPrintService.java

Print this page


   1 /*
   2  * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 564                 int[] finArray = attribClass.getArrayOfIntValues();
 565                 if ((finArray != null) && (finArray.length > 0)) {
 566                     Finishings[] finSup = new Finishings[finArray.length];
 567                     for (int i=0; i<finArray.length; i++) {
 568                         finSup[i] = Finishings.NONE;
 569                         Finishings[] fAll = (Finishings[])
 570                             (new ExtFinishing(100)).getAll();
 571                         for (int j=0; j<fAll.length; j++) {
 572                             if (finArray[i] == fAll[j].getValue()) {
 573                                 finSup[i] = fAll[j];
 574                                 break;
 575                             }
 576                         }
 577                     }
 578                     return finSup;
 579                 }
 580             }
 581         } else if (category == JobName.class) {
 582             return new JobName("Java Printing", null);
 583         } else if (category == JobSheets.class) {
 584             JobSheets arr[] = new JobSheets[2];
 585             arr[0] = JobSheets.NONE;
 586             arr[1] = JobSheets.STANDARD;
 587             return arr;
 588 
 589         } else if (category == Media.class) {
 590             Media[] allMedia = new Media[mediaSizeNames.length+
 591                                         mediaTrays.length];
 592 
 593             for (int i=0; i<mediaSizeNames.length; i++) {
 594                 allMedia[i] = mediaSizeNames[i];
 595             }
 596 
 597             for (int i=0; i<mediaTrays.length; i++) {
 598                 allMedia[i+mediaSizeNames.length] = mediaTrays[i];
 599             }
 600 
 601             if (allMedia.length == 0) {
 602                 allMedia = new Media[1];
 603                 allMedia[0] = (Media)getDefaultAttributeValue(Media.class);
 604             }


1741                            }
1742                        }
1743                     }
1744                 } catch (java.io.IOException e) {
1745                     debug_println(" isPostscript, e= "+e);
1746                     /* if PPD is not found, this may be a raw printer
1747                        and in this case it is assumed that it is a
1748                        Postscript printer */
1749                     // do nothing
1750                 }
1751             }
1752         }
1753         return isPS.booleanValue();
1754     }
1755 
1756 
1757     private void opGetAttributes() {
1758         try {
1759             debug_println(debugPrefix+"opGetAttributes myURI "+myURI+" myURL "+myURL);
1760 
1761             AttributeClass attClNoUri[] = {
1762                 AttributeClass.ATTRIBUTES_CHARSET,
1763                 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE};
1764 
1765             AttributeClass attCl[] = {
1766                 AttributeClass.ATTRIBUTES_CHARSET,
1767                 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
1768                 new AttributeClass("printer-uri",
1769                                    AttributeClass.TAG_URI,
1770                                    ""+myURI)};
1771 
1772             OutputStream os = java.security.AccessController.
1773                 doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
1774                     public OutputStream run() {
1775                         try {
1776                             return urlConnection.getOutputStream();
1777                         } catch (Exception e) {
1778                         }
1779                         return null;
1780                     }
1781                 });
1782 
1783             if (os == null) {
1784                 return;
1785             }


1867                 // string is all we need.
1868             }
1869 
1870             osw.write(GRPTAG_END_ATTRIBUTES);
1871             osw.flush();
1872             osw.close();
1873         } catch (java.io.IOException ioe) {
1874             debug_println(debugPrefix+"writeIPPRequest, IPPPrintService Exception in writeIPPRequest: "+ioe);
1875             return false;
1876         }
1877         return true;
1878     }
1879 
1880 
1881     public static HashMap<String, AttributeClass>[] readIPPResponse(InputStream inputStream) {
1882 
1883         if (inputStream == null) {
1884             return null;
1885         }
1886 
1887         byte response[] = new byte[MAX_ATTRIBUTE_LENGTH];
1888         try {
1889 
1890             DataInputStream ois = new DataInputStream(inputStream);
1891 
1892             // read status and ID
1893             if ((ois.read(response, 0, 8) > -1) &&
1894                 (response[2] == STATUSCODE_SUCCESS)) {
1895 
1896                 ByteArrayOutputStream outObj;
1897                 int counter=0;
1898                 short len = 0;
1899                 String attribStr = null;
1900                 // assign default value
1901                 byte valTagByte = AttributeClass.TAG_KEYWORD;
1902                 ArrayList<HashMap<String, AttributeClass>> respList = new ArrayList<>();
1903                 HashMap<String, AttributeClass> responseMap = new HashMap<>();
1904 
1905                 response[0] = ois.readByte();
1906 
1907                 // check for group tags


1914                     outObj = new ByteArrayOutputStream();
1915                     //make sure counter and attribStr are re-initialized
1916                     counter = 0;
1917                     attribStr = null;
1918 
1919                     // read value tag
1920                     response[0] = ois.readByte();
1921                     while (response[0] >= AttributeClass.TAG_UNSUPPORTED_VALUE &&
1922                            response[0] <= AttributeClass.TAG_MEMBER_ATTRNAME) {
1923                         // read name length
1924                         len  = ois.readShort();
1925 
1926                         // If current value is not part of previous attribute
1927                         // then close stream and add it to HashMap.
1928                         // It is part of previous attribute if name length=0.
1929                         if ((len != 0) && (attribStr != null)) {
1930                             //last byte is the total # of values
1931                             outObj.write(counter);
1932                             outObj.flush();
1933                             outObj.close();
1934                             byte outArray[] = outObj.toByteArray();
1935 
1936                             // if key exists, new HashMap
1937                             if (responseMap.containsKey(attribStr)) {
1938                                 respList.add(responseMap);
1939                                 responseMap = new HashMap<>();
1940                             }
1941 
1942                             // exclude those that are unknown
1943                             if (valTagByte >= AttributeClass.TAG_INT) {
1944                                 AttributeClass ac =
1945                                     new AttributeClass(attribStr,
1946                                                        valTagByte,
1947                                                        outArray);
1948 
1949                                 responseMap.put(ac.getName(), ac);
1950                                 debug_println(debugPrefix+ "readIPPResponse "+ac);
1951                             }
1952 
1953                             outObj = new ByteArrayOutputStream();
1954                             counter = 0; //reset counter


1978                         ois.read(response, 0, len);
1979                         // write value of "len" length
1980                         outObj.write(response, 0, len);
1981                         counter++;
1982                         // read next byte
1983                         response[0] = ois.readByte();
1984                     }
1985 
1986                     if (attribStr != null) {
1987                         outObj.write(counter);
1988                         outObj.flush();
1989                         outObj.close();
1990 
1991                         // if key exists in old HashMap, new HashMap
1992                         if ((counter != 0) &&
1993                             responseMap.containsKey(attribStr)) {
1994                             respList.add(responseMap);
1995                             responseMap = new HashMap<>();
1996                         }
1997 
1998                         byte outArray[] = outObj.toByteArray();
1999 
2000                         AttributeClass ac =
2001                             new AttributeClass(attribStr,
2002                                                valTagByte,
2003                                                outArray);
2004                         responseMap.put(ac.getName(), ac);
2005                     }
2006                 }
2007                 ois.close();
2008                 if ((responseMap != null) && (responseMap.size() > 0)) {
2009                     respList.add(responseMap);
2010                 }
2011                 @SuppressWarnings({"unchecked", "rawtypes"})
2012                 HashMap<String, AttributeClass>[] tmp  =
2013                     respList.toArray((HashMap<String, AttributeClass>[])new HashMap[respList.size()]);
2014                 return tmp;
2015             } else {
2016                 debug_println(debugPrefix+
2017                           "readIPPResponse client error, IPP status code: 0x"+
2018                           toHex(response[2]) + toHex(response[3]));


   1 /*
   2  * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


 564                 int[] finArray = attribClass.getArrayOfIntValues();
 565                 if ((finArray != null) && (finArray.length > 0)) {
 566                     Finishings[] finSup = new Finishings[finArray.length];
 567                     for (int i=0; i<finArray.length; i++) {
 568                         finSup[i] = Finishings.NONE;
 569                         Finishings[] fAll = (Finishings[])
 570                             (new ExtFinishing(100)).getAll();
 571                         for (int j=0; j<fAll.length; j++) {
 572                             if (finArray[i] == fAll[j].getValue()) {
 573                                 finSup[i] = fAll[j];
 574                                 break;
 575                             }
 576                         }
 577                     }
 578                     return finSup;
 579                 }
 580             }
 581         } else if (category == JobName.class) {
 582             return new JobName("Java Printing", null);
 583         } else if (category == JobSheets.class) {
 584             JobSheets[] arr = new JobSheets[2];
 585             arr[0] = JobSheets.NONE;
 586             arr[1] = JobSheets.STANDARD;
 587             return arr;
 588 
 589         } else if (category == Media.class) {
 590             Media[] allMedia = new Media[mediaSizeNames.length+
 591                                         mediaTrays.length];
 592 
 593             for (int i=0; i<mediaSizeNames.length; i++) {
 594                 allMedia[i] = mediaSizeNames[i];
 595             }
 596 
 597             for (int i=0; i<mediaTrays.length; i++) {
 598                 allMedia[i+mediaSizeNames.length] = mediaTrays[i];
 599             }
 600 
 601             if (allMedia.length == 0) {
 602                 allMedia = new Media[1];
 603                 allMedia[0] = (Media)getDefaultAttributeValue(Media.class);
 604             }


1741                            }
1742                        }
1743                     }
1744                 } catch (java.io.IOException e) {
1745                     debug_println(" isPostscript, e= "+e);
1746                     /* if PPD is not found, this may be a raw printer
1747                        and in this case it is assumed that it is a
1748                        Postscript printer */
1749                     // do nothing
1750                 }
1751             }
1752         }
1753         return isPS.booleanValue();
1754     }
1755 
1756 
1757     private void opGetAttributes() {
1758         try {
1759             debug_println(debugPrefix+"opGetAttributes myURI "+myURI+" myURL "+myURL);
1760 
1761             AttributeClass[] attClNoUri = {
1762                 AttributeClass.ATTRIBUTES_CHARSET,
1763                 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE};
1764 
1765             AttributeClass[] attCl = {
1766                 AttributeClass.ATTRIBUTES_CHARSET,
1767                 AttributeClass.ATTRIBUTES_NATURAL_LANGUAGE,
1768                 new AttributeClass("printer-uri",
1769                                    AttributeClass.TAG_URI,
1770                                    ""+myURI)};
1771 
1772             OutputStream os = java.security.AccessController.
1773                 doPrivileged(new java.security.PrivilegedAction<OutputStream>() {
1774                     public OutputStream run() {
1775                         try {
1776                             return urlConnection.getOutputStream();
1777                         } catch (Exception e) {
1778                         }
1779                         return null;
1780                     }
1781                 });
1782 
1783             if (os == null) {
1784                 return;
1785             }


1867                 // string is all we need.
1868             }
1869 
1870             osw.write(GRPTAG_END_ATTRIBUTES);
1871             osw.flush();
1872             osw.close();
1873         } catch (java.io.IOException ioe) {
1874             debug_println(debugPrefix+"writeIPPRequest, IPPPrintService Exception in writeIPPRequest: "+ioe);
1875             return false;
1876         }
1877         return true;
1878     }
1879 
1880 
1881     public static HashMap<String, AttributeClass>[] readIPPResponse(InputStream inputStream) {
1882 
1883         if (inputStream == null) {
1884             return null;
1885         }
1886 
1887         byte[] response = new byte[MAX_ATTRIBUTE_LENGTH];
1888         try {
1889 
1890             DataInputStream ois = new DataInputStream(inputStream);
1891 
1892             // read status and ID
1893             if ((ois.read(response, 0, 8) > -1) &&
1894                 (response[2] == STATUSCODE_SUCCESS)) {
1895 
1896                 ByteArrayOutputStream outObj;
1897                 int counter=0;
1898                 short len = 0;
1899                 String attribStr = null;
1900                 // assign default value
1901                 byte valTagByte = AttributeClass.TAG_KEYWORD;
1902                 ArrayList<HashMap<String, AttributeClass>> respList = new ArrayList<>();
1903                 HashMap<String, AttributeClass> responseMap = new HashMap<>();
1904 
1905                 response[0] = ois.readByte();
1906 
1907                 // check for group tags


1914                     outObj = new ByteArrayOutputStream();
1915                     //make sure counter and attribStr are re-initialized
1916                     counter = 0;
1917                     attribStr = null;
1918 
1919                     // read value tag
1920                     response[0] = ois.readByte();
1921                     while (response[0] >= AttributeClass.TAG_UNSUPPORTED_VALUE &&
1922                            response[0] <= AttributeClass.TAG_MEMBER_ATTRNAME) {
1923                         // read name length
1924                         len  = ois.readShort();
1925 
1926                         // If current value is not part of previous attribute
1927                         // then close stream and add it to HashMap.
1928                         // It is part of previous attribute if name length=0.
1929                         if ((len != 0) && (attribStr != null)) {
1930                             //last byte is the total # of values
1931                             outObj.write(counter);
1932                             outObj.flush();
1933                             outObj.close();
1934                             byte[] outArray = outObj.toByteArray();
1935 
1936                             // if key exists, new HashMap
1937                             if (responseMap.containsKey(attribStr)) {
1938                                 respList.add(responseMap);
1939                                 responseMap = new HashMap<>();
1940                             }
1941 
1942                             // exclude those that are unknown
1943                             if (valTagByte >= AttributeClass.TAG_INT) {
1944                                 AttributeClass ac =
1945                                     new AttributeClass(attribStr,
1946                                                        valTagByte,
1947                                                        outArray);
1948 
1949                                 responseMap.put(ac.getName(), ac);
1950                                 debug_println(debugPrefix+ "readIPPResponse "+ac);
1951                             }
1952 
1953                             outObj = new ByteArrayOutputStream();
1954                             counter = 0; //reset counter


1978                         ois.read(response, 0, len);
1979                         // write value of "len" length
1980                         outObj.write(response, 0, len);
1981                         counter++;
1982                         // read next byte
1983                         response[0] = ois.readByte();
1984                     }
1985 
1986                     if (attribStr != null) {
1987                         outObj.write(counter);
1988                         outObj.flush();
1989                         outObj.close();
1990 
1991                         // if key exists in old HashMap, new HashMap
1992                         if ((counter != 0) &&
1993                             responseMap.containsKey(attribStr)) {
1994                             respList.add(responseMap);
1995                             responseMap = new HashMap<>();
1996                         }
1997 
1998                         byte[] outArray = outObj.toByteArray();
1999 
2000                         AttributeClass ac =
2001                             new AttributeClass(attribStr,
2002                                                valTagByte,
2003                                                outArray);
2004                         responseMap.put(ac.getName(), ac);
2005                     }
2006                 }
2007                 ois.close();
2008                 if ((responseMap != null) && (responseMap.size() > 0)) {
2009                     respList.add(responseMap);
2010                 }
2011                 @SuppressWarnings({"unchecked", "rawtypes"})
2012                 HashMap<String, AttributeClass>[] tmp  =
2013                     respList.toArray((HashMap<String, AttributeClass>[])new HashMap[respList.size()]);
2014                 return tmp;
2015             } else {
2016                 debug_println(debugPrefix+
2017                           "readIPPResponse client error, IPP status code: 0x"+
2018                           toHex(response[2]) + toHex(response[3]));


< prev index next >