├── .github └── workflows │ └── gradle.yml ├── .gitignore ├── LICENSE ├── README.md ├── build.gradle ├── demo ├── A4-blank.pdf ├── go └── printjob.jar ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── settings.gradle └── src └── main └── java └── ipp └── IppPrinter.java /.github/workflows/gradle.yml: -------------------------------------------------------------------------------- 1 | # This workflow will build a Java project with Gradle 2 | # https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-java-with-gradle 3 | 4 | name: Java CI with Gradle 5 | 6 | on: 7 | push: 8 | branches: [ master ] 9 | pull_request: 10 | branches: [ master ] 11 | 12 | jobs: 13 | build: 14 | runs-on: ubuntu-latest 15 | steps: 16 | - uses: actions/checkout@v3 17 | 18 | - uses: actions/setup-java@v3 19 | with: 20 | distribution: 'temurin' 21 | java-version: '11' 22 | 23 | # https://github.com/gradle/wrapper-validation-action 24 | - uses: gradle/wrapper-validation-action@v1 25 | 26 | # https://github.com/gradle/gradle-build-action 27 | - uses: gradle/gradle-build-action@v2.4.2 28 | 29 | - name: Build with Gradle Wrapper 30 | run: ./gradlew build 31 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled class file 2 | *.class 3 | 4 | # Log file 5 | *.log 6 | 7 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 8 | hs_err_pid* 9 | 10 | # Gradle 11 | /.gradle 12 | 13 | # Idea 14 | /.idea 15 | /build 16 | 17 | # misc 18 | .DS_Store 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Gerhard Muth 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # ipp-printjob-java 3 | A minimal ipp protocol implementation (about 100 lines) to submit a document to a printer. 4 | 5 | ![Java CI with Gradle](https://github.com/gmuth/ipp-printjob-java/workflows/Java%20CI%20with%20Gradle/badge.svg) 6 | 7 | ### General 8 | 9 | The printjob source code should be useful for use cases of driverless printing. 10 | Especially automated processes without user interaction should benefit from simple solutions like this. 11 | This code is not and will never become a full-fledged ipp implementation. 12 | I provide more control over print jobs, monitoring of printers and print jobs in my other project 13 | [ipp-client-kotlin](https://github.com/gmuth/ipp-client-kotlin). 14 | 15 | ### Demonstration 16 | 17 | Directory `demo` contains the `printjob.jar` and a test script called `go` that submits a blank PDF to Apple's Printer Simulator. 18 | To avoid real printing, registered Apple developers can download 19 | [Additional Tools for Xcode](https://developer.apple.com/download/all/?q=hardware) 20 | containing the Printer Simulator. 21 | 22 | ### Usage 23 | 24 | The tool takes two arguments: *printer-uri* and *file-name*. 25 | If you don't know the printer uri try `ippfind`. 26 | 27 | ``` 28 | java -jar printjob.jar ipp://colorjet:631/ipp/printer A4-blank.pdf 29 | 30 | send A4-blank.pdf to ipp://colorjet:631/ipp/printer 31 | version 1.1 32 | status 0x0000 33 | group 01 34 | attributes-charset (47) = us-ascii 35 | attributes-natural-language (48) = en 36 | group 02 37 | job-uri (45) = ipp://colorjet:631/jobs/352 38 | job-id (21) = 352 39 | job-state (23) = 3 40 | job-state-reasons (44) = none 41 | group 03 42 | ``` 43 | 44 | The equivalent java code is: 45 | 46 | ```java 47 | new IppPrinter(new URI("ipp://colorjet:631/ipp/printer")).printJob(File("A4-blank.pdf")); 48 | ``` 49 | ### Document Format 50 | 51 | The operation attributes group does not include a value for `document-format` by default. 52 | This should be equivalent to `application/octet-stream` indicating the printer has to auto sense the document format. 53 | You have to make sure the printer supports the document format you send - PDF is usually a good option. 54 | If required by your printer, you can set the document format programmatically by adding it e.g. after the `printer-uri` attribute. 55 | 56 | writeAttribute(0x49, "document-format", "application/pdf"); 57 | 58 | ### Issues 59 | 60 | If you use an unsupported `printer-uri` you might get a response similar to this one: 61 | 62 | ``` 63 | send A4-blank.pdf to ipp://localhost:8632/ipp/norona 64 | version 1.1 65 | status 0x0400 66 | requestId 1 67 | group 01 68 | attributes-charset (0x47) = utf-8 69 | attributes-natural-language (0x48) = en-us 70 | status-message (0x41) = Bad printer-uri "ipp://localhost:8632/ipp/norona". 71 | group 03 72 | ``` 73 | You can use `ippfind` or `dns-sd -Z _ipp._tcp` (look at the rp value) to discover your printer's uri. 74 | If you have other issues contact me. 75 | 76 | ### Build 77 | 78 | To build `printjob.jar` into `build/libs` you need an installed JDK. 79 | 80 | ./gradlew 81 | 82 | ### Community 83 | 84 | I'd be happy to see this minimal ipp implementation being ported to all kinds of programming languages. 85 | On request I'll explain developers without experience in jvm based languages what the jvm runtime library is used for. 86 | -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // How to build? 2 | // $ ./gradlew 3 | // 4 | // How to run? 5 | // $ java -jar build/libs/printjob.jar ipp://localhost:8632/printers/laser 6 | 7 | // update gradle version: ./gradlew wrapper --gradle-version 7.6.2 8 | 9 | plugins { 10 | id "java-library" 11 | } 12 | 13 | repositories { 14 | mavenCentral() 15 | } 16 | 17 | defaultTasks("clean", "assemble") 18 | 19 | java { 20 | sourceCompatibility = JavaVersion.VERSION_11 21 | targetCompatibility = JavaVersion.VERSION_11 22 | } 23 | 24 | jar { 25 | manifest { 26 | attributes 'Main-Class': 'ipp.IppPrinter' 27 | } 28 | archivesBaseName = 'printjob' 29 | } -------------------------------------------------------------------------------- /demo/A4-blank.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmuth/ipp-printjob-java/35d633f5d0a4285f51d31ad762efbf0e7c696972/demo/A4-blank.pdf -------------------------------------------------------------------------------- /demo/go: -------------------------------------------------------------------------------- 1 | java -jar printjob.jar ipp://localhost:8632/printers/laser A4-blank.pdf 2 | -------------------------------------------------------------------------------- /demo/printjob.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmuth/ipp-printjob-java/35d633f5d0a4285f51d31ad762efbf0e7c696972/demo/printjob.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gmuth/ipp-printjob-java/35d633f5d0a4285f51d31ad762efbf0e7c696972/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | distributionBase=GRADLE_USER_HOME 2 | distributionPath=wrapper/dists 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.2-bin.zip 4 | networkTimeout=10000 5 | zipStoreBase=GRADLE_USER_HOME 6 | zipStorePath=wrapper/dists 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # 4 | # Copyright © 2015-2021 the original authors. 5 | # 6 | # Licensed under the Apache License, Version 2.0 (the "License"); 7 | # you may not use this file except in compliance with the License. 8 | # You may obtain a copy of the License at 9 | # 10 | # https://www.apache.org/licenses/LICENSE-2.0 11 | # 12 | # Unless required by applicable law or agreed to in writing, software 13 | # distributed under the License is distributed on an "AS IS" BASIS, 14 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 | # See the License for the specific language governing permissions and 16 | # limitations under the License. 17 | # 18 | 19 | ############################################################################## 20 | # 21 | # Gradle start up script for POSIX generated by Gradle. 22 | # 23 | # Important for running: 24 | # 25 | # (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is 26 | # noncompliant, but you have some other compliant shell such as ksh or 27 | # bash, then to run this script, type that shell name before the whole 28 | # command line, like: 29 | # 30 | # ksh Gradle 31 | # 32 | # Busybox and similar reduced shells will NOT work, because this script 33 | # requires all of these POSIX shell features: 34 | # * functions; 35 | # * expansions «$var», «${var}», «${var:-default}», «${var+SET}», 36 | # «${var#prefix}», «${var%suffix}», and «$( cmd )»; 37 | # * compound commands having a testable exit status, especially «case»; 38 | # * various built-in commands including «command», «set», and «ulimit». 39 | # 40 | # Important for patching: 41 | # 42 | # (2) This script targets any POSIX shell, so it avoids extensions provided 43 | # by Bash, Ksh, etc; in particular arrays are avoided. 44 | # 45 | # The "traditional" practice of packing multiple parameters into a 46 | # space-separated string is a well documented source of bugs and security 47 | # problems, so this is (mostly) avoided, by progressively accumulating 48 | # options in "$@", and eventually passing that to Java. 49 | # 50 | # Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS, 51 | # and GRADLE_OPTS) rely on word-splitting, this is performed explicitly; 52 | # see the in-line comments for details. 53 | # 54 | # There are tweaks for specific operating systems such as AIX, CygWin, 55 | # Darwin, MinGW, and NonStop. 56 | # 57 | # (3) This script is generated from the Groovy template 58 | # https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt 59 | # within the Gradle project. 60 | # 61 | # You can find Gradle at https://github.com/gradle/gradle/. 62 | # 63 | ############################################################################## 64 | 65 | # Attempt to set APP_HOME 66 | 67 | # Resolve links: $0 may be a link 68 | app_path=$0 69 | 70 | # Need this for daisy-chained symlinks. 71 | while 72 | APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path 73 | [ -h "$app_path" ] 74 | do 75 | ls=$( ls -ld "$app_path" ) 76 | link=${ls#*' -> '} 77 | case $link in #( 78 | /*) app_path=$link ;; #( 79 | *) app_path=$APP_HOME$link ;; 80 | esac 81 | done 82 | 83 | # This is normally unused 84 | # shellcheck disable=SC2034 85 | APP_BASE_NAME=${0##*/} 86 | APP_HOME=$( cd "${APP_HOME:-./}" && pwd -P ) || exit 87 | 88 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 89 | DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"' 90 | 91 | # Use the maximum available, or set MAX_FD != -1 to use that value. 92 | MAX_FD=maximum 93 | 94 | warn () { 95 | echo "$*" 96 | } >&2 97 | 98 | die () { 99 | echo 100 | echo "$*" 101 | echo 102 | exit 1 103 | } >&2 104 | 105 | # OS specific support (must be 'true' or 'false'). 106 | cygwin=false 107 | msys=false 108 | darwin=false 109 | nonstop=false 110 | case "$( uname )" in #( 111 | CYGWIN* ) cygwin=true ;; #( 112 | Darwin* ) darwin=true ;; #( 113 | MSYS* | MINGW* ) msys=true ;; #( 114 | NONSTOP* ) nonstop=true ;; 115 | esac 116 | 117 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 118 | 119 | 120 | # Determine the Java command to use to start the JVM. 121 | if [ -n "$JAVA_HOME" ] ; then 122 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 123 | # IBM's JDK on AIX uses strange locations for the executables 124 | JAVACMD=$JAVA_HOME/jre/sh/java 125 | else 126 | JAVACMD=$JAVA_HOME/bin/java 127 | fi 128 | if [ ! -x "$JAVACMD" ] ; then 129 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 130 | 131 | Please set the JAVA_HOME variable in your environment to match the 132 | location of your Java installation." 133 | fi 134 | else 135 | JAVACMD=java 136 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 137 | 138 | Please set the JAVA_HOME variable in your environment to match the 139 | location of your Java installation." 140 | fi 141 | 142 | # Increase the maximum file descriptors if we can. 143 | if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then 144 | case $MAX_FD in #( 145 | max*) 146 | # In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked. 147 | # shellcheck disable=SC3045 148 | MAX_FD=$( ulimit -H -n ) || 149 | warn "Could not query maximum file descriptor limit" 150 | esac 151 | case $MAX_FD in #( 152 | '' | soft) :;; #( 153 | *) 154 | # In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked. 155 | # shellcheck disable=SC3045 156 | ulimit -n "$MAX_FD" || 157 | warn "Could not set maximum file descriptor limit to $MAX_FD" 158 | esac 159 | fi 160 | 161 | # Collect all arguments for the java command, stacking in reverse order: 162 | # * args from the command line 163 | # * the main class name 164 | # * -classpath 165 | # * -D...appname settings 166 | # * --module-path (only if needed) 167 | # * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables. 168 | 169 | # For Cygwin or MSYS, switch paths to Windows format before running java 170 | if "$cygwin" || "$msys" ; then 171 | APP_HOME=$( cygpath --path --mixed "$APP_HOME" ) 172 | CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" ) 173 | 174 | JAVACMD=$( cygpath --unix "$JAVACMD" ) 175 | 176 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 177 | for arg do 178 | if 179 | case $arg in #( 180 | -*) false ;; # don't mess with options #( 181 | /?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath 182 | [ -e "$t" ] ;; #( 183 | *) false ;; 184 | esac 185 | then 186 | arg=$( cygpath --path --ignore --mixed "$arg" ) 187 | fi 188 | # Roll the args list around exactly as many times as the number of 189 | # args, so each arg winds up back in the position where it started, but 190 | # possibly modified. 191 | # 192 | # NB: a `for` loop captures its iteration list before it begins, so 193 | # changing the positional parameters here affects neither the number of 194 | # iterations, nor the values presented in `arg`. 195 | shift # remove old arg 196 | set -- "$@" "$arg" # push replacement arg 197 | done 198 | fi 199 | 200 | # Collect all arguments for the java command; 201 | # * $DEFAULT_JVM_OPTS, $JAVA_OPTS, and $GRADLE_OPTS can contain fragments of 202 | # shell script including quotes and variable substitutions, so put them in 203 | # double quotes to make sure that they get re-expanded; and 204 | # * put everything else in single quotes, so that it's not re-expanded. 205 | 206 | set -- \ 207 | "-Dorg.gradle.appname=$APP_BASE_NAME" \ 208 | -classpath "$CLASSPATH" \ 209 | org.gradle.wrapper.GradleWrapperMain \ 210 | "$@" 211 | 212 | # Stop when "xargs" is not available. 213 | if ! command -v xargs >/dev/null 2>&1 214 | then 215 | die "xargs is not available" 216 | fi 217 | 218 | # Use "xargs" to parse quoted args. 219 | # 220 | # With -n1 it outputs one arg per line, with the quotes and backslashes removed. 221 | # 222 | # In Bash we could simply go: 223 | # 224 | # readarray ARGS < <( xargs -n1 <<<"$var" ) && 225 | # set -- "${ARGS[@]}" "$@" 226 | # 227 | # but POSIX shell has neither arrays nor command substitution, so instead we 228 | # post-process each arg (as a line of input to sed) to backslash-escape any 229 | # character that might be a shell metacharacter, then use eval to reverse 230 | # that process (while maintaining the separation between arguments), and wrap 231 | # the whole thing up as a single "set" statement. 232 | # 233 | # This will of course break if any of these variables contains a newline or 234 | # an unmatched quote. 235 | # 236 | 237 | eval "set -- $( 238 | printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" | 239 | xargs -n1 | 240 | sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' | 241 | tr '\n' ' ' 242 | )" '"$@"' 243 | 244 | exec "$JAVACMD" "$@" 245 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @rem 2 | @rem Copyright 2015 the original author or authors. 3 | @rem 4 | @rem Licensed under the Apache License, Version 2.0 (the "License"); 5 | @rem you may not use this file except in compliance with the License. 6 | @rem You may obtain a copy of the License at 7 | @rem 8 | @rem https://www.apache.org/licenses/LICENSE-2.0 9 | @rem 10 | @rem Unless required by applicable law or agreed to in writing, software 11 | @rem distributed under the License is distributed on an "AS IS" BASIS, 12 | @rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | @rem See the License for the specific language governing permissions and 14 | @rem limitations under the License. 15 | @rem 16 | 17 | @if "%DEBUG%"=="" @echo off 18 | @rem ########################################################################## 19 | @rem 20 | @rem Gradle startup script for Windows 21 | @rem 22 | @rem ########################################################################## 23 | 24 | @rem Set local scope for the variables with windows NT shell 25 | if "%OS%"=="Windows_NT" setlocal 26 | 27 | set DIRNAME=%~dp0 28 | if "%DIRNAME%"=="" set DIRNAME=. 29 | @rem This is normally unused 30 | set APP_BASE_NAME=%~n0 31 | set APP_HOME=%DIRNAME% 32 | 33 | @rem Resolve any "." and ".." in APP_HOME to make it shorter. 34 | for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi 35 | 36 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 37 | set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m" 38 | 39 | @rem Find java.exe 40 | if defined JAVA_HOME goto findJavaFromJavaHome 41 | 42 | set JAVA_EXE=java.exe 43 | %JAVA_EXE% -version >NUL 2>&1 44 | if %ERRORLEVEL% equ 0 goto execute 45 | 46 | echo. 47 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 48 | echo. 49 | echo Please set the JAVA_HOME variable in your environment to match the 50 | echo location of your Java installation. 51 | 52 | goto fail 53 | 54 | :findJavaFromJavaHome 55 | set JAVA_HOME=%JAVA_HOME:"=% 56 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 57 | 58 | if exist "%JAVA_EXE%" goto execute 59 | 60 | echo. 61 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 62 | echo. 63 | echo Please set the JAVA_HOME variable in your environment to match the 64 | echo location of your Java installation. 65 | 66 | goto fail 67 | 68 | :execute 69 | @rem Setup the command line 70 | 71 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 72 | 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %* 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if %ERRORLEVEL% equ 0 goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | set EXIT_CODE=%ERRORLEVEL% 85 | if %EXIT_CODE% equ 0 set EXIT_CODE=1 86 | if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE% 87 | exit /b %EXIT_CODE% 88 | 89 | :mainEnd 90 | if "%OS%"=="Windows_NT" endlocal 91 | 92 | :omega 93 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = 'ipp-printjob-java' -------------------------------------------------------------------------------- /src/main/java/ipp/IppPrinter.java: -------------------------------------------------------------------------------- 1 | package ipp; 2 | 3 | // Author: Gerhard Muth 4 | 5 | import java.io.*; 6 | import java.net.HttpURLConnection; 7 | import java.net.URI; 8 | 9 | class IppPrinter { 10 | 11 | public static void main(final String[] args) { 12 | try { 13 | // printer uri for PrinterSimulator v87 (Apple) 14 | URI uri = new URI("ipp://localhost:8632/ipp/print/laser"); 15 | File file = new File("demo/A4-blank.pdf"); 16 | if (args.length > 0) uri = new URI(args[0]); 17 | if (args.length > 1) file = new File(args[1]); 18 | new IppPrinter(uri).printJob(file); 19 | } catch (Exception exception) { 20 | exception.printStackTrace(System.err); 21 | } 22 | } 23 | 24 | private URI uri; 25 | private DataOutputStream dataOutputStream; 26 | private DataInputStream dataInputStream; 27 | 28 | IppPrinter(URI uri) { 29 | this.uri = uri; 30 | } 31 | 32 | // https://tools.ietf.org/html/rfc8011#section-4.2.1 33 | public void printJob(final File file) throws IOException { 34 | System.out.printf("send %s to %s%n", file.getName(), uri); 35 | String httpScheme = uri.getScheme().replace("ipp", "http"); 36 | URI httpUri = URI.create(String.format("%s:%s", httpScheme, uri.getRawSchemeSpecificPart())); 37 | HttpURLConnection httpUrlConnection = (HttpURLConnection) httpUri.toURL().openConnection(); 38 | httpUrlConnection.setConnectTimeout(5000); 39 | httpUrlConnection.setDoOutput(true); 40 | httpUrlConnection.setRequestProperty("Content-Type", "application/ipp"); 41 | 42 | // rfc 8010 syntax of encoding: https://tools.ietf.org/html/rfc8010#page-15 43 | dataOutputStream = new DataOutputStream(httpUrlConnection.getOutputStream()); 44 | dataOutputStream.writeShort(0x0101); // ipp version 1.1 45 | // operation -> https://tools.ietf.org/html/rfc8011#section-5.4.15 46 | dataOutputStream.writeShort(0x0002); // operation Print-Job 47 | dataOutputStream.writeInt(0x0001); // request id 48 | dataOutputStream.writeByte(0x01); // operation group tag 49 | writeAttribute(0x47, "attributes-charset", "us-ascii"); // charset tag 50 | writeAttribute(0x48, "attributes-natural-language", "en"); // natural-language tag 51 | writeAttribute(0x45, "printer-uri", uri.toString()); // uri tag 52 | dataOutputStream.writeByte(0x03); // end tag 53 | new FileInputStream(file) {{ 54 | transferTo(dataOutputStream); 55 | close(); 56 | }}; 57 | 58 | // check http response 59 | if (httpUrlConnection.getResponseCode() != 200) { 60 | System.err.println(new String(httpUrlConnection.getErrorStream().readAllBytes())); 61 | throw new IOException(String.format("post to %s failed with http status %d", uri, httpUrlConnection.getResponseCode())); 62 | } 63 | if (!"application/ipp".equals(httpUrlConnection.getHeaderField("Content-Type"))) { 64 | throw new IOException("response type is not ipp"); 65 | } 66 | 67 | // decode ipp response 68 | dataInputStream = new DataInputStream(httpUrlConnection.getInputStream()); 69 | System.out.printf("version %d.%d%n", dataInputStream.readByte(), dataInputStream.readByte()); 70 | // status -> https://www.iana.org/assignments/ipp-registrations/ipp-registrations.xhtml#ipp-registrations-11 71 | System.out.printf("status 0x%04X%n", dataInputStream.readShort()); 72 | System.out.printf("requestId %d%n", dataInputStream.readInt()); 73 | byte tag; 74 | do { 75 | tag = dataInputStream.readByte(); 76 | // delimiter tag -> https://tools.ietf.org/html/rfc8010#section-3.5.1 77 | if (tag < 0x10) { 78 | System.out.printf("group %02X%n", tag); 79 | continue; 80 | } 81 | String name = readStringValue(); 82 | Object value; 83 | // value tag -> https://tools.ietf.org/html/rfc8010#section-3.5.2 84 | switch (tag) { 85 | case 0x21: // integer 86 | case 0x23: // enum 87 | dataInputStream.readShort(); // value length: 4 88 | value = dataInputStream.readInt(); 89 | break; 90 | case 0x41: // textWithoutLanguage 91 | case 0x44: // keyword 92 | case 0x45: // uri 93 | case 0x47: // charset 94 | case 0x48: // naturalLanguage 95 | value = readStringValue(); 96 | break; 97 | default: 98 | readStringValue(); 99 | value = ""; 100 | } 101 | System.out.printf(" %s (0x%02X) = %s%n", name, tag, value); 102 | } while (tag != (byte) 0x03); // end tag 103 | // job-state -> https://tools.ietf.org/html/rfc8011#section-5.3.7 104 | dataOutputStream.close(); 105 | dataInputStream.close(); 106 | } 107 | 108 | private void writeAttribute(final Integer tag, final String name, final String value) throws IOException { 109 | dataOutputStream.writeByte(tag); 110 | dataOutputStream.writeShort(name.length()); 111 | dataOutputStream.write(name.getBytes()); 112 | dataOutputStream.writeShort(value.length()); 113 | dataOutputStream.write(value.getBytes()); 114 | } 115 | 116 | private String readStringValue() throws IOException { 117 | byte[] valueBytes = dataInputStream.readNBytes(dataInputStream.readShort()); 118 | return new String(valueBytes, "us-ascii"); 119 | } 120 | } --------------------------------------------------------------------------------