001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.dataaccess.impl;
017    
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.io.OutputStream;
021    import java.io.OutputStreamWriter;
022    import java.io.UnsupportedEncodingException;
023    import java.net.HttpURLConnection;
024    import java.net.MalformedURLException;
025    import java.net.ProtocolException;
026    import java.net.URL;
027    
028    import org.kuali.kfs.module.purap.dataaccess.B2BDao;
029    import org.kuali.kfs.module.purap.exception.B2BConnectionException;
030    
031    public class B2BDaoImpl implements B2BDao {
032        private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(B2BDaoImpl.class);
033    
034        /**
035         * Take the request XML, post it to SciQuest, then get the response XML and return it.
036         */
037        public String sendPunchOutRequest(String request, String punchoutUrl) {
038            LOG.debug("sendPunchOutRequest() started");
039    
040            try {
041                URL url = new URL(punchoutUrl);
042                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
043    
044                conn.setDoInput(true);
045                conn.setDoOutput(true);
046                conn.setRequestMethod("POST");
047                conn.setRequestProperty("Content-type", "text/xml");
048    
049                OutputStream out = conn.getOutputStream();
050                OutputStreamWriter outw = new OutputStreamWriter(out, "UTF-8");
051                outw.write(request);
052                outw.flush();
053                outw.close();
054                out.flush();
055                out.close();
056    
057                InputStream inp = conn.getInputStream();
058    
059                StringBuffer response = new StringBuffer();
060                int i = inp.read();
061                while (i >= 0) {
062                    if (i >= 0) {
063                        response.append((char) i);
064                    }
065                    i = inp.read();
066                }
067                return response.toString();
068            }
069            catch (MalformedURLException e) {
070                LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
071                throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
072            }
073            catch (ProtocolException e) {
074                LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
075                throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
076            }
077            catch (UnsupportedEncodingException e) {
078                LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
079                throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
080            }
081            catch (IOException e) {
082                LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
083                throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
084            }
085        }
086    }