use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.
the class SOAPCommunicator method getHeaders.
/**
* Returns mime headers in HTTP servlet request.
*
* @param req HTTP servlet request.
* @return mime headers in HTTP servlet request.
*/
public MimeHeaders getHeaders(final HttpServletRequest req) {
Enumeration<String> e = req.getHeaderNames();
MimeHeaders headers = new MimeHeaders();
while (e.hasMoreElements()) {
String headerName = e.nextElement();
String headerValue = req.getHeader(headerName);
debug.message("SOAPCommunicator.getHeaders: Header name={}, value={}", headerName, headerValue);
StringTokenizer values = new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens()) {
headers.addHeader(headerName, values.nextToken().trim());
}
}
if (debug.messageEnabled()) {
debug.message("SOAPCommunicator.getHeaders: Header=" + headers.toString());
}
return headers;
}
use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.
the class FSSSOAndFedService method doPost.
/**
* Processes single sign on POST request.
* @param request <code>HttpServletRequest</code> object
* @param response <code>HttpServletResponse</code> object
* @exception ServletException, IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FSUtils.debug.message("FSSSOAndFedService.doPost: Called");
if ((request == null) || (response == null)) {
response.sendError(response.SC_INTERNAL_SERVER_ERROR, FSUtils.bundle.getString("nullInputParameter"));
return;
}
if (FSUtils.needSetLBCookieAndRedirect(request, response, true)) {
return;
}
// Check if it's an LECP request
if (isLECPRequest(request)) {
// TODO: assume auth framework will understand this param
String useForward = (String) request.getAttribute(Constants.FORWARD_PARAM);
if (useForward != null && useForward.equals(Constants.FORWARD_YES_VALUE)) {
// this is a forward POST after authentication, need to
// use GET instead of POST here
FSUtils.debug.message("FSSSOAndFedService.doPost: LECP forward");
this.doGet(request, response);
} else {
try {
MimeHeaders mimeHeaders = SAMLUtils.getMimeHeaders(request);
ServletInputStream sInputStream = request.getInputStream();
SOAPMessage soapMessage = msgFactory.createMessage(mimeHeaders, sInputStream);
this.onMessage(request, response, soapMessage);
} catch (SOAPException se) {
throw new ServletException(se);
}
}
return;
}
// obtain AuthnRequest message
String enocodedAuthnRequest = request.getParameter(IFSConstants.POST_AUTHN_REQUEST_PARAM);
if (enocodedAuthnRequest == null) {
doGet(request, response);
return;
}
enocodedAuthnRequest = enocodedAuthnRequest.replace(' ', '\n');
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("FSSSOAndFedService.doPost: " + "BASE64 encoded AuthnRequest at the RECEIVER: " + enocodedAuthnRequest);
}
//decode and create FSAuthnRequest object
FSAuthnRequest authnRequest = null;
try {
authnRequest = FSAuthnRequest.parseBASE64EncodedString(enocodedAuthnRequest);
if (authnRequest == null) {
FSUtils.debug.error("FSSSOAndFedService: " + FSUtils.bundle.getString("invalidAuthnRequest"));
String[] data = { FSUtils.bundle.getString("invalidAuthnRequest") };
LogUtil.error(Level.INFO, LogUtil.INVALID_AUTHN_REQUEST, data);
response.sendError(response.SC_BAD_REQUEST, FSUtils.bundle.getString("invalidAuthnRequest"));
return;
} else {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("FSSSOAndFedService: " + "AuthnRequest received:" + authnRequest.toXMLString());
}
}
} catch (FSException e) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("FSSSOAndFedService: " + FSUtils.bundle.getString("invalidAuthnRequest"), e);
}
response.sendError(response.SC_BAD_REQUEST, FSUtils.bundle.getString("invalidAuthnRequest"));
return;
}
String metaAlias = null;
String realm = null;
String hostEntityId = null;
IDPDescriptorType hostedDesc = null;
BaseConfigType hostedConfig = null;
try {
metaAlias = FSServiceUtils.getMetaAlias(request);
realm = IDFFMetaUtils.getRealmByMetaAlias(metaAlias);
hostEntityId = metaManager.getEntityIDByMetaAlias(metaAlias);
hostedDesc = metaManager.getIDPDescriptor(realm, hostEntityId);
hostedConfig = metaManager.getIDPDescriptorConfig(realm, hostEntityId);
} catch (Exception e) {
if (FSUtils.debug.messageEnabled()) {
FSUtils.debug.message("FSSSOAndFedService: couldn't obtain hosted entity id:", e);
}
}
handleAuthnRequest(request, response, authnRequest, false, false, realm, hostEntityId, metaAlias, hostedDesc, hostedConfig);
return;
}
use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.
the class SAMLSOAPReceiver method doPost.
/**
* Processes request coming from SOAP.
*
* @param req <code>HttpServletRequest</code> object.
* @param resp <code>HttpServletResponse</code> object.
* @throws ServletException if there is an error.
* @throws IOException if there is an error.
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException {
if (SAMLUtils.getMaxContentLength() != 0) {
int length = req.getContentLength();
if (length == -1) {
throw new ServletException(SAMLUtils.bundle.getString("unknownLength"));
}
if (length > SAMLUtils.getMaxContentLength()) {
if (SAMLUtils.debug.messageEnabled()) {
SAMLUtils.debug.message("content length too large" + length);
}
throw new ServletException(SAMLUtils.bundle.getString("largeContentLength"));
}
}
String remoteAddr = ClientUtils.getClientIPAddress(req);
Set partnerSourceID = null;
if ((partnerSourceID = checkCaller(req, resp)) != null) {
if (SAMLUtils.debug.messageEnabled()) {
SAMLUtils.debug.message(" got request from a trusted server, " + "processing it now..");
}
try {
MimeHeaders mimeHeaders = SAMLUtils.getMimeHeaders(req);
ServletInputStream sInputStream = req.getInputStream();
//Create the SOAPMessage from the reply
SOAPMessage soapMessage = msgFactory.createMessage(mimeHeaders, sInputStream);
SOAPMessage soapMessageReply = null;
soapMessageReply = this.onMessage(req, resp, soapMessage, partnerSourceID);
if (soapMessageReply != null) {
if (soapMessageReply.saveRequired())
soapMessageReply.saveChanges();
//Check to see if presence of SOAPFault
if (containsFault(soapMessageReply)) {
if (SAMLUtils.debug.messageEnabled()) {
SAMLUtils.debug.message("Contains a SOAPFault!");
}
resp.setStatus(resp.SC_INTERNAL_SERVER_ERROR);
} else {
resp.setStatus(resp.SC_OK);
}
//Send the response back to the senderby placing
//the mime headers into the response, and
//externalizing the soapmessage onto the response object
SAMLUtils.setMimeHeaders(soapMessageReply.getMimeHeaders(), resp);
ServletOutputStream sOutputStream = resp.getOutputStream();
soapMessageReply.writeTo(sOutputStream);
sOutputStream.flush();
}
} catch (Exception e) {
throw new ServletException(e);
}
} else {
// its not trusted site
SAMLUtils.debug.error("Error message from SOAP Receiver:" + remoteAddr + " is untrusted site");
String[] data = { SAMLUtils.bundle.getString("untrustedSite"), remoteAddr };
LogUtils.error(java.util.logging.Level.INFO, LogUtils.UNTRUSTED_SITE, data);
SOAPMessage faultReply = FormSOAPError(resp, "Server", "untrustedSite", null);
SAMLUtils.setMimeHeaders(faultReply.getMimeHeaders(), resp);
ServletOutputStream sOutputStream = resp.getOutputStream();
try {
faultReply.writeTo(sOutputStream);
} catch (SOAPException se) {
throw new ServletException(se);
}
sOutputStream.flush();
}
}
use of javax.xml.soap.MimeHeaders in project OpenAM by OpenRock.
the class SAMLUtils method getMimeHeaders.
/**
* Returns a <code>MimeHeaders</code> object that contains the headers
* in the given <code>HttpServletRequest</code> object.
*
* @param req the <code>HttpServletRequest</code> object.
* @return a new <code>MimeHeaders</code> object containing the headers.
*/
public static MimeHeaders getMimeHeaders(HttpServletRequest req) {
MimeHeaders headers = new MimeHeaders();
if (req == null) {
debug.message("SAMLUtils.getMimeHeaders: null input");
return headers;
}
Enumeration enumerator = req.getHeaderNames();
while (enumerator.hasMoreElements()) {
String headerName = (String) enumerator.nextElement();
String headerValue = req.getHeader(headerName);
StringTokenizer values = new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens()) {
headers.addHeader(headerName, values.nextToken().trim());
}
}
return headers;
}
use of javax.xml.soap.MimeHeaders in project tdi-studio-se by Talend.
the class SOAPUtil method extractContentAsDocument.
/**
* invoke soap and return the response document
*/
public String extractContentAsDocument(String version, String destination, String soapAction, String soapMessage) throws SOAPException, TransformerException, ParserConfigurationException, FileNotFoundException, UnsupportedEncodingException {
MessageFactory messageFactory = null;
if (version.equals(SOAP12)) {
messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
} else {
messageFactory = MessageFactory.newInstance();
}
SOAPMessage message = messageFactory.createMessage();
MimeHeaders mimeHeaders = message.getMimeHeaders();
mimeHeaders.setHeader("SOAPAction", soapAction);
SOAPPart soapPart = message.getSOAPPart();
String encoding = getEncoding(soapMessage);
ByteArrayInputStream stream = new ByteArrayInputStream(soapMessage.getBytes(encoding));
StreamSource preppedMsgSrc = new StreamSource(stream);
soapPart.setContent(preppedMsgSrc);
message.saveChanges();
SOAPMessage reply = connection.call(message, destination);
SOAPPart reSoapPart = reply.getSOAPPart();
if (reSoapPart != null && reSoapPart instanceof SOAPPartImpl) {
((SOAPPartImpl) reSoapPart).setSourceCharsetEncoding(encoding);
}
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
t.transform(reSoapPart.getContent(), new StreamResult(bos));
return bos.toString(encoding);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
Aggregations