View Javadoc
1   /*
2    * Copyright 2007-2008 The Kuali Foundation
3    * 
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    * http://www.opensource.org/licenses/ecl2.php
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.dataaccess.impl;
17  
18  import org.kuali.ole.module.purap.dataaccess.B2BDao;
19  import org.kuali.ole.module.purap.exception.B2BConnectionException;
20  import org.kuali.rice.core.framework.persistence.ojb.dao.PlatformAwareDaoBaseOjb;
21  
22  import java.io.*;
23  import java.net.HttpURLConnection;
24  import java.net.MalformedURLException;
25  import java.net.ProtocolException;
26  import java.net.URL;
27  
28  public class B2BDaoImpl extends PlatformAwareDaoBaseOjb implements B2BDao {
29      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(B2BDaoImpl.class);
30  
31      /**
32       * Take the request XML, post it to SciQuest, then get the response XML and return it.
33       */
34      public String sendPunchOutRequest(String request, String punchoutUrl) {
35          LOG.debug("sendPunchOutRequest() started");
36  
37          try {
38              URL url = new URL(punchoutUrl);
39              HttpURLConnection conn = (HttpURLConnection) url.openConnection();
40  
41              conn.setDoInput(true);
42              conn.setDoOutput(true);
43              conn.setRequestMethod("POST");
44              conn.setRequestProperty("Content-type", "text/xml");
45  
46              OutputStream out = conn.getOutputStream();
47              OutputStreamWriter outw = new OutputStreamWriter(out, "UTF-8");
48              outw.write(request);
49              outw.flush();
50              outw.close();
51              out.flush();
52              out.close();
53  
54              InputStream inp = conn.getInputStream();
55  
56              StringBuffer response = new StringBuffer();
57              int i = inp.read();
58              while (i >= 0) {
59                  if (i >= 0) {
60                      response.append((char) i);
61                  }
62                  i = inp.read();
63              }
64              return response.toString();
65          } catch (MalformedURLException e) {
66              LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
67              throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
68          } catch (ProtocolException e) {
69              LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
70              throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
71          } catch (UnsupportedEncodingException e) {
72              LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
73              throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
74          } catch (IOException e) {
75              LOG.error("postPunchOutSetupRequestMessage() Error posting setup", e);
76              throw new B2BConnectionException("Unable to connect to remote site for punchout.", e);
77          }
78      }
79  }