com.ptc.windchill.ws
Class HttpUtils

java.lang.Object
  extended bycom.ptc.windchill.ws.HttpUtils

public class HttpUtils
extends Object

Utility class that can be used to upload or download content from the Windchill Web Services API.

Example Download

 // get an instance of ContentHandle from the GetDownloadHandles Windchill Web Services method.
 ContentHandle handle = ...
 HttpUtils utils = new HttpUtils();
 String username, password;
 username = password = null;
 for (;;) {
   try {
     if ( username != null ) utils.setUsername ( username );
     if ( password != null ) utils.setPassword ( password );
     InputStream is = utils.download ( handle );
     // read the data from is
     break;
   } catch ( IOException ioe ) {
     if ( utils.getRc() != HttpUtils.HTTP_UNAUTHORIZED )
       throw ioe;
     else {
       // prompt the user for credentials, set the username/password variables and try again.
       // the getHost()/getRealm() methods can be used to build a cache to avoid subsequent unauthenticated requests.
     }
   }
 }
 

Example Upload

 // get an instance of ContentHandle from the GetUploadHandles Windchill Web Services method.
 ContentHandle handle = ...
 File f = ...
 HttpUtils utils = new HttpUtils();
 String username, password;
 username = password = null;
 for (;;) {
   try {
     if ( username != null ) utils.setUsername ( username );
     if ( password != null ) utils.setPassword ( password );
     utils.upload ( handle, f ).close();
     // ignore response from server as long as HTTP_OK
     break;
   } catch ( IOException ioe ) {
     if ( utils.getRc() != HttpUtils.HTTP_UNAUTHORIZED )
       throw ioe;
     else {
       // prompt the user for credentials, set the username/password variables and try again.
       // the getHost()/getRealm() methods can be used to build a cache to avoid subsequent unauthenticated requests.
     }
   }
 }
 


Field Summary
private static int BUF_SIZE
           
private  HttpURLConnection connection
           
private static String CONNECTION_HEADER
           
private static String CONTENT_DISPOSITION_HEADER
           
private static String CONTENT_TYPE_HEADER
           
private static String DEFAULT_CONTENT_TYPE
           
private static int DOWNLOADING
           
static int HTTP_INTERNAL_ERROR
          HTTP Internal Server Error
static int HTTP_OK
          HTTP OK
static int HTTP_UNAUTHORIZED
          HTTP Unauthorized
private  String password
           
private  int rc
           
private  int STATE
           
private static int UNKNOWN
           
private static int UPLOADING
           
private  URL url
           
private  String username
           
private static String WWW_AUTH_HEADER
           
 
Constructor Summary
HttpUtils()
          Creates an HttpUtils object.
HttpUtils(String username, String password)
          Creates an HttpUtils object.
 
Method Summary
 InputStream download(ContentHandle handle)
          Downloads data from a ContentHandle.
 int getContentLength()
          Gets the content length of data the server responded with.
 String getHost()
          Gets the host that upload or download was last performed to.
 String getPassword()
          Gets the password that will be sent with requests.
 int getRc()
          Gets the response code issued by the web server in response to a request.
 String getRealm()
          Gets the realm.
 String getUsername()
          Gets the username that will be sent with requests.
private  boolean isUsASCII(String s)
          Determins whether a string contains only printable US-ASCII characters or not.
static void main(String[] args)
           
 void setPassword(String password)
          Sets the password that will be sent with requests.
 void setUsername(String username)
          Sets the username that will be sent with requests.
 InputStream upload(ContentHandle handle, javax.activation.DataSource data)
          Uploads data from a DataSource to a ContentHandle.
 InputStream upload(ContentHandle handle, File file)
          Uploads data from a File to a ContentHandle.
 InputStream upload(ContentHandle handle, URL url)
          Uploads data from a URL to a ContentHandle.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

username

private String username

password

private String password

url

private URL url

connection

private HttpURLConnection connection

rc

private int rc

STATE

private int STATE

UNKNOWN

private static final int UNKNOWN
See Also:
Constant Field Values

DOWNLOADING

private static final int DOWNLOADING
See Also:
Constant Field Values

UPLOADING

private static final int UPLOADING
See Also:
Constant Field Values

CONTENT_TYPE_HEADER

private static final String CONTENT_TYPE_HEADER
See Also:
Constant Field Values

CONTENT_DISPOSITION_HEADER

private static final String CONTENT_DISPOSITION_HEADER
See Also:
Constant Field Values

WWW_AUTH_HEADER

private static final String WWW_AUTH_HEADER
See Also:
Constant Field Values

CONNECTION_HEADER

private static final String CONNECTION_HEADER
See Also:
Constant Field Values

DEFAULT_CONTENT_TYPE

private static final String DEFAULT_CONTENT_TYPE
See Also:
Constant Field Values

BUF_SIZE

private static final int BUF_SIZE
See Also:
Constant Field Values

HTTP_OK

public static final int HTTP_OK
HTTP OK

See Also:
Constant Field Values

HTTP_UNAUTHORIZED

public static final int HTTP_UNAUTHORIZED
HTTP Unauthorized

See Also:
Constant Field Values

HTTP_INTERNAL_ERROR

public static final int HTTP_INTERNAL_ERROR
HTTP Internal Server Error

See Also:
Constant Field Values
Constructor Detail

HttpUtils

public HttpUtils()
Creates an HttpUtils object. The object created will issue unauthenticated requests unless the setUsername(String)/setPassword(String) methods are called.

See Also:
setUsername(String), setPassword(String)

HttpUtils

public HttpUtils(String username,
                 String password)
Creates an HttpUtils object. The object created will issue authenticated requests.

Parameters:
username - The username.
password - The password.
Method Detail

setUsername

public void setUsername(String username)
Sets the username that will be sent with requests. Only basic authentication is supported.

Parameters:
username - The username.
See Also:
setPassword(String)

getUsername

public String getUsername()
Gets the username that will be sent with requests.

Returns:
The username.

setPassword

public void setPassword(String password)
Sets the password that will be sent with requests. Only basic authentication is supported.

Parameters:
password - The password.
See Also:
setUsername(String)

getPassword

public String getPassword()
Gets the password that will be sent with requests.

Returns:
The password.

getRc

public int getRc()
Gets the response code issued by the web server in response to a request.

Returns:
The response code.
Throws:
IllegalStateException - If the call is not following a call to upload or download where the server was contacted.
See Also:
HTTP_OK, HTTP_UNAUTHORIZED, HTTP_INTERNAL_ERROR, HttpURLConnection

getContentLength

public int getContentLength()
Gets the content length of data the server responded with. Can only be called following a call to download and prior to invoking another download/upload method.

Returns:
The content length.
Throws:
IllegalStateException - If call is not subsequent to a call to download.

getHost

public String getHost()
Gets the host that upload or download was last performed to. Can only be called following a call to upload or download.

Returns:
The host the ContentHandle referred the upload or download request to.
Throws:
IllegalStateException - If call is not following upload or download.
See Also:
getRealm()

getRealm

public String getRealm()
                throws IOException
Gets the realm. Can be called following an unauthorized response from the server (getRc() == HTTP_UNAUTHORIZED). Only Basic authentication is supported. Can only be called following a call to upload or download and following an unauthorized server response.

Returns:
The realm.
Throws:
IllegalStateException - If call is not following upload or download.
IOException - If the response is unexpected or there is an issue with the WWW-Authenticate header.
See Also:
getRc(), getHost()

download

public InputStream download(ContentHandle handle)
                     throws IOException
Downloads data from a ContentHandle.

Parameters:
handle - The ContentHandle.
Throws:
IOException - If an error occurs.
See Also:
getRc()

upload

public InputStream upload(ContentHandle handle,
                          javax.activation.DataSource data)
                   throws IOException
Uploads data from a DataSource to a ContentHandle.

Parameters:
handle - The ContentHandle.
data - The DataSource object.
Returns:
An InputStream that can be used to read the response from the web server,
Throws:
IOException - If an error occurs.
See Also:
getRc()

upload

public InputStream upload(ContentHandle handle,
                          File file)
                   throws IOException
Uploads data from a File to a ContentHandle.

Parameters:
handle - The ContentHandle.
file - The File object.
Returns:
An InputStream that can be used to read the response from the web server,
Throws:
IOException - If an error occurs.
See Also:
getRc()

upload

public InputStream upload(ContentHandle handle,
                          URL url)
                   throws IOException
Uploads data from a URL to a ContentHandle.

Parameters:
handle - The ContentHandle.
url - The URL object.
Returns:
An InputStream that can be used to read the response from the web server,
Throws:
IOException - If an error occurs.
See Also:
getRc()

isUsASCII

private boolean isUsASCII(String s)
Determins whether a string contains only printable US-ASCII characters or not.

Parameters:
s - The string to examine.
Returns:
true if it contains only printibal US-ASCII characters.

main

public static void main(String[] args)
                 throws Exception
Throws:
Exception