Package com.ptc.windchill.ws
Class HttpUtils
java.lang.Object
com.ptc.windchill.ws.HttpUtils
Utility class that can be used to upload or download content from the Windchill Web Services API.
Supported API: true
Extendable: false
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.
}
}
}
Supported API: true
Extendable: false
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptiondownload(ContentHandle handle) Downloads data from a ContentHandle.intGets the content length of data the server responded with.getHost()Gets the host that upload or download was last performed to.Gets the password that will be sent with requests.intgetRc()Gets the response code issued by the web server in response to a request.getRealm()Gets the realm.Gets the username that will be sent with requests.voidsetPassword(String password) Sets the password that will be sent with requests.voidsetUsername(String username) Sets the username that will be sent with requests.upload(ContentHandle handle, File file) Uploads data from a File to a ContentHandle.upload(ContentHandle handle, URL url) Uploads data from a URL to a ContentHandle.upload(ContentHandle handle, javax.activation.DataSource data) Uploads data from a DataSource to a ContentHandle.
-
Constructor Details
-
HttpUtils
public HttpUtils()Creates an HttpUtils object. The object created will issue unauthenticated requests unless thesetUsername(String)/setPassword(String)methods are called.
Supported API: true- See Also:
-
HttpUtils
Creates an HttpUtils object. The object created will issue authenticated requests.
Supported API: true- Parameters:
username- The username.password- The password.
-
-
Method Details
-
setUsername
Sets the username that will be sent with requests. Only basic authentication is supported.
Supported API: true- Parameters:
username- The username.- See Also:
-
getUsername
Gets the username that will be sent with requests.
Supported API: true- Returns:
- The username.
-
setPassword
Sets the password that will be sent with requests. Only basic authentication is supported.
Supported API: true- Parameters:
password- The password.- See Also:
-
getPassword
Gets the password that will be sent with requests.
Supported API: true- Returns:
- The password.
-
getRc
public int getRc()Gets the response code issued by the web server in response to a request.
Supported API: true- 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:
-
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.
Supported API: true- Returns:
- The content length.
- Throws:
IllegalStateException- If call is not subsequent to a call to download.
-
getHost
Gets the host that upload or download was last performed to. Can only be called following a call to upload or download.
Supported API: true- 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
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.
Supported API: true- 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:
-
download
Downloads data from a ContentHandle.
Supported API: true- Parameters:
handle- The ContentHandle.- Throws:
IOException- If an error occurs.- See Also:
-
upload
public InputStream upload(ContentHandle handle, javax.activation.DataSource data) throws IOException Uploads data from a DataSource to a ContentHandle.
Supported API: true- 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:
-
upload
Uploads data from a File to a ContentHandle.
Supported API: true- 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:
-
upload
Uploads data from a URL to a ContentHandle.
Supported API: true- 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:
-