use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class NetStorageSender method extractResult.
@Override
public String extractResult(HttpResponseHandler responseHandler, ParameterResolutionContext prc) throws SenderException, IOException {
int statusCode = responseHandler.getStatusLine().getStatusCode();
boolean ok = false;
if (StringUtils.isNotEmpty(getResultStatusCodeSessionKey())) {
prc.getSession().put(getResultStatusCodeSessionKey(), Integer.toString(statusCode));
ok = true;
} else {
if (statusCode == HttpServletResponse.SC_OK) {
ok = true;
} else {
if (isIgnoreRedirects()) {
if (statusCode == HttpServletResponse.SC_MOVED_PERMANENTLY || statusCode == HttpServletResponse.SC_MOVED_TEMPORARILY || statusCode == HttpServletResponse.SC_TEMPORARY_REDIRECT) {
ok = true;
}
}
}
}
if (!ok) {
throw new SenderException(getLogPrefix() + "httpstatus " + statusCode + ": " + responseHandler.getStatusLine().getReasonPhrase() + " body: " + getResponseBodyAsString(responseHandler));
}
XmlBuilder result = new XmlBuilder("result");
HttpServletResponse response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
if (response == null) {
XmlBuilder statuscode = new XmlBuilder("statuscode");
statuscode.setValue(statusCode + "");
result.addSubElement(statuscode);
String responseString = getResponseBodyAsString(responseHandler);
responseString = XmlUtils.skipDocTypeDeclaration(responseString.trim());
responseString = XmlUtils.skipXmlDeclaration(responseString);
if (statusCode == HttpURLConnection.HTTP_OK) {
XmlBuilder message = new XmlBuilder("message");
message.setValue(responseString, false);
result.addSubElement(message);
} else {
// Validate Server-Time drift
String dateString = responseHandler.getHeader("Date");
if (!StringUtils.isEmpty(dateString)) {
Date currentDate = new Date();
DateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH);
long responseDate = 0;
try {
Date date = format.parse(dateString);
responseDate = date.getTime();
} catch (Exception e) {
}
if (responseDate != 0 && currentDate.getTime() - responseDate > 30 * 1000)
throw new SenderException("Local server Date is more than 30s out of sync with Remote server");
}
XmlBuilder message = new XmlBuilder("error");
message.setValue(responseString);
result.addSubElement(message);
log.warn(String.format("Unexpected Response from Server: %d %s\n%s", statusCode, responseString, responseHandler.getHeaderFields()));
}
}
return result.toXML();
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class NetStorageSender method buildUri.
/**
* Builds the URI with the rootDirectory, optional CpCode and makes sure the
* path never ends with a slash '/'.
* @param path to append to the root
* @return full path to use as endpoint
* @throws SenderException
*/
private URIBuilder buildUri(String path) throws SenderException {
if (!path.startsWith("/"))
path = "/" + path;
try {
String url = getUrl() + getCpCode();
if (getRootDir() != null)
url += getRootDir();
url += path;
if (// The path should never end with a '/'
url.endsWith("/"))
url = url.substring(0, url.length() - 1);
return new URIBuilder(url);
} catch (URISyntaxException e) {
throw new SenderException(e);
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class CmisHttpSender method getMethod.
@Override
public HttpRequestBase getMethod(URIBuilder uri, String message, ParameterValueList pvl, Map<String, String> headersParamsMap, IPipeLineSession session) throws SenderException {
HttpRequestBase method = null;
try {
if (getMethodType().equals("GET")) {
method = new HttpGet(uri.build());
} else if (getMethodType().equals("POST")) {
HttpPost httpPost = new HttpPost(uri.build());
// send data
if (pvl.getParameterValue("writer") != null) {
Output writer = (Output) pvl.getParameterValue("writer").getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
httpPost.setHeader("Content-Encoding", "gzip");
writer.write(new GZIPOutputStream(out, 4096));
} else {
writer.write(out);
}
HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
httpPost.setEntity(entity);
out.close();
method = httpPost;
}
} else if (getMethodType().equals("PUT")) {
HttpPut httpPut = new HttpPut(uri.build());
// send data
if (pvl.getParameterValue("writer") != null) {
Output writer = (Output) pvl.getParameterValue("writer").getValue();
ByteArrayOutputStream out = new ByteArrayOutputStream();
Object clientCompression = pvl.getParameterValue(SessionParameter.CLIENT_COMPRESSION);
if ((clientCompression != null) && Boolean.parseBoolean(clientCompression.toString())) {
httpPut.setHeader("Content-Encoding", "gzip");
writer.write(new GZIPOutputStream(out, 4096));
} else {
writer.write(out);
}
HttpEntity entity = new BufferedHttpEntity(new ByteArrayEntity(out.toByteArray()));
httpPut.setEntity(entity);
out.close();
method = httpPut;
}
} else if (getMethodType().equals("DELETE")) {
method = new HttpDelete(uri.build());
} else {
throw new MethodNotSupportedException("method [" + getMethodType() + "] not implemented");
}
} catch (Exception e) {
throw new SenderException(e);
}
for (Map.Entry<String, String> entry : headers.entrySet()) {
log.debug("append header [" + entry.getKey() + "] with value [" + entry.getValue() + "]");
method.addHeader(entry.getKey(), entry.getValue());
}
// Cmis creates it's own contentType depending on the method and bindingType
method.setHeader("Content-Type", getContentType());
log.debug(getLogPrefix() + "HttpSender constructed " + getMethodType() + "-method [" + method.getURI() + "] query [" + method.getURI().getQuery() + "] ");
return method;
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class CmisSender method sendMessageForActionGet.
private String sendMessageForActionGet(String correlationID, String message, ParameterResolutionContext prc) throws SenderException, TimeOutException {
if (StringUtils.isEmpty(message)) {
throw new SenderException(getLogPrefix() + "input string cannot be empty but must contain a documentId");
}
CmisObject object = null;
try {
object = session.getObject(session.createObjectId(message));
} catch (CmisObjectNotFoundException e) {
if (StringUtils.isNotEmpty(getResultOnNotFound())) {
log.info(getLogPrefix() + "document with id [" + message + "] not found", e);
return getResultOnNotFound();
} else {
throw new SenderException(e);
}
}
Document document = (Document) object;
ContentStream contentStream = document.getContentStream();
try {
InputStream inputStream = contentStream.getStream();
if (isStreamResultToServlet()) {
HttpServletResponse response = (HttpServletResponse) prc.getSession().get(IPipeLineSession.HTTP_RESPONSE_KEY);
String contentType = contentStream.getMimeType();
if (StringUtils.isNotEmpty(contentType)) {
log.debug(getLogPrefix() + "setting response Content-Type header [" + contentType + "]");
response.setHeader("Content-Type", contentType);
}
String contentDisposition = "attachment; filename=\"" + contentStream.getFileName() + "\"";
log.debug(getLogPrefix() + "setting response Content-Disposition header [" + contentDisposition + "]");
response.setHeader("Content-Disposition", contentDisposition);
OutputStream outputStream;
outputStream = response.getOutputStream();
Misc.streamToStream(inputStream, outputStream);
log.debug(getLogPrefix() + "copied document content input stream [" + inputStream + "] to output stream [" + outputStream + "]");
return "";
} else if (isGetProperties()) {
if (StringUtils.isNotEmpty(fileInputStreamSessionKey)) {
prc.getSession().put(getFileInputStreamSessionKey(), inputStream);
} else {
byte[] bytes = Misc.streamToBytes(inputStream);
prc.getSession().put(getFileContentSessionKey(), Base64.encodeBase64String(bytes));
}
XmlBuilder cmisXml = new XmlBuilder("cmis");
XmlBuilder propertiesXml = new XmlBuilder("properties");
for (Iterator it = document.getProperties().iterator(); it.hasNext(); ) {
Property property = (Property) it.next();
propertiesXml.addSubElement(getPropertyXml(property));
}
cmisXml.addSubElement(propertiesXml);
return cmisXml.toXML();
} else {
return Misc.streamToString(inputStream, null, false);
}
} catch (IOException e) {
throw new SenderException(e);
}
}
use of nl.nn.adapterframework.core.SenderException in project iaf by ibissource.
the class CmisSender method getSession.
public Session getSession(ParameterResolutionContext prc) throws SenderException {
if (session == null || !isKeepSession()) {
String authAlias_work = null;
String userName_work = null;
String password_work = null;
ParameterValueList pvl = null;
try {
if (prc != null && paramList != null) {
pvl = prc.getValues(paramList);
if (pvl != null) {
ParameterValue pv = pvl.getParameterValue("authAlias");
if (pv != null) {
authAlias_work = (String) pv.getValue();
}
pv = pvl.getParameterValue("userName");
if (pv != null) {
userName_work = (String) pv.getValue();
}
pv = pvl.getParameterValue("password");
if (pv != null) {
password_work = (String) pv.getValue();
}
}
}
} catch (ParameterException e) {
throw new SenderException(getLogPrefix() + "Sender [" + getName() + "] caught exception evaluating parameters", e);
}
if (authAlias_work == null) {
authAlias_work = getAuthAlias();
}
if (userName_work == null) {
userName_work = getUserName();
}
if (password_work == null) {
password_work = getPassword();
}
CredentialFactory cf = new CredentialFactory(authAlias_work, userName_work, password_work);
session = connect(cf.getUsername(), cf.getPassword());
}
return session;
}
Aggregations