基于Java的Lotus代理如何更改内容类型

时间:2015-11-24 08:56:58

标签: java xml lotus-domino lotus

我在Lotus notes中有一个java代理,我收到一个xml并解析它。 这是代码:

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Arrays;

public class JavaAgent extends AgentBase {
    private static PrintWriter pw;
    public void NotesMain() {


        try {
             Session session = getSession();
             AgentContext agentContext = session.getAgentContext();
             Database db = agentContext.getCurrentDatabase();

             //domino 03
             boolean ProductieDatabase = false;

             String ApplicatieServer = "CN=Server01";
             String OrderDB = "General\\Order.nsf";
             String TestOrderDB = "General\\TestOrder.nsf";

             String Relations= "General\\Relations.nsf";
             String Tabel= "General\\Tabel.nsf";

             org.w3c.dom.Document domdoc = null;

             lotus.domino.Document doc = agentContext.getDocumentContext();
             Item requestContent = null;
             StringBuffer sb = new StringBuffer();
             pw = getAgentOutput();

             //Open databases

             Database RelationDB= session.getDatabase(ApplicatieServer,Relations, false);

             Database TabelDB= session.getDatabase(ApplicatieServer,Tabel, false);

             //Open order database
              Database TabelDB;
              if(ProductieDatabase == true)
              {
                TabelDB= session.getDatabase(ApplicatieServer,OrderDB, false);
              }
              else
              {
                TabelDB= session.getDatabase(ApplicatieServer,TestOrderDB, false);
              }

              //Maak nieuw request document aan
              lotus.domino.Document RequestDoc = db.createDocument();
              //Alle velden uit de http post toevoegen aan document
              Vector items = doc.getItems();
              for (int j=0; j<items.size(); j++)
                {
                 // System.out.println ("Testorders 3");
                  Item item = (Item)items.elementAt(j);
                  String fldName = item.getName();
                  String fldValue = item.getValueString();
                  RequestDoc.replaceItemValue(fldName, fldValue);
                  if ( fldName.matches("(?i).*request_content.*") )
                    {
                      sb.append( fldValue );

                    }
                }  

              RequestDoc.replaceItemValue("Form", "Response");
              RequestDoc.replaceItemValue("Status", "0");
              RequestDoc.replaceItemValue("ID_Request",  RequestDoc.getUniversalID());

              Date currentDate=new Date(System.currentTimeMillis());
              SimpleDateFormat formatterDatum=new SimpleDateFormat("yyyyMMdd");
              SimpleDateFormat formatterTijd=new SimpleDateFormat("HHmmss");

              String HubwooBestandspad = "C:\\OCI\\asd\\";
              String Bestnaam = cxvxcv+ "asdasd" + RequestDoc.getUniversalID() + "_" + formatterDatum.format(currentDate) + "_" + formatterTijd.format(currentDate) + ".xml";


              Stream outStream = session.createStream();
              if (outStream.open(Bestnaam, "ASCII")) {
                if (outStream.getBytes() == 0) {
                  outStream.writeText(sb.toString());
                  outStream.close();

                }
                else
                  System.out.println("Output file exists and has content");
              }
              else{
                System.out.println("Output file open failed");
              }


                File input = new File(Bestnaam);
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                domdoc = dBuilder.parse(input);


                domdoc.getDocumentElement().normalize();

                NodeList nList = domdoc.getElementsByTagName("ItemOut");
                String test = "TEST OCI AGENT";

                for (int temp = 0; temp < nList.getLength(); temp++) {

                    Node nNode = nList.item(temp);
                    Element eElement = (Element) nNode;

                    System.out.println("\nCurrent Element :" + nList.getLength());
                    test = test + "**Delivery Date**" +  eElement.getAttribute("requestedDeliveryDate") + "!END! [" + temp + "]";
                    test = test + " " +  eElement.getElementsByTagName("SupplierPartID").item(0).getTextContent();
                    test = test + " " +  eElement.getElementsByTagName("Description").item(0).getTextContent();


                }
                RequestDoc.replaceItemValue("JSON", sb.toString());
                RequestDoc.replaceItemValue("JSON", test);
                // OM HELE XML FILE TE KRIJGEN 
                RequestDoc.replaceItemValue("XmlTU",sb.toString());
              RequestDoc.save();


              //verander content-type
              pw.println("Content-Type: text/xml");
              //verander charset
              pw.println("charset: UFT-8");
              //stuur 200 OK terug
              pw.println("200 OK");





        }
    catch(Exception e) {
            e.printStackTrace();
        }
    }


}

如何将内容类型成功更改为text / xml,将charset更改为utf-8。我正在考虑创建一个扩展HttpServlet的额外类,它具有doPost方法。但是,我怎样才能将该对象实现到此代理中? 现在这个服务工作,使用firefox海报我也收到响应200 OK以及content-type = text / xml ..但是如果从.net envorinment调用此服务,则.net显示协议违规错误。

有人可以帮忙吗?

  • .net应用程序的代码:

     Public Function SubmitOrder(ByVal OrderXML, ByVal URL)
      Dim objXMLHTTP  'MSXML2.ServerXMLHTTP
      'We provide our own error handling
      On Error Resume Next
    
      'The MSXML2.ServerXMLHTTP object allows xml to be posted to
      'external webpages.
      Set objXMLHTTP = CreateObject("MSXML2.ServerXMLHTTP.6.0")
      If Err.Number <> 0 Then
        SubmitOrder = "ERROR: Failed to create ServerXMLHTTP - " & Err.Description
        Exit Function
      End If   
    
      'Specify the webpage we wish to use
      objXMLHTTP.Open "POST", URL, False
      If Err.Number <> 0 Then
        SubmitOrder = "ERROR: Failed to open URL - " & Err.Description
        Exit Function
      End If
    
      'The type of information we are sending
      objXMLHTTP.setRequestHeader "Content-Type", "text/xml"
      If Err.Number <> 0 Then
        SubmitOrder = "ERROR: Failed to setRequestHeader - " & Err.Description
        Exit Function
      End If
    
      'Send the information
                objXMLHTTP.send replace(OrderXML,"UTF-16","UTF-8")
      If Err.Number <> 0 Then
         SubmitOrder = "ERROR: Failed to send data to " & URL & " - " & Err.Description & ", Error number --> " & Err.Number
        Exit Function
       End If
    
    
    'Return the result
      SubmitOrder = objXMLHTTP.responseText
    end Function
    
    public Function CheckSuccess(ByVal Response)
    
      'Check for any internal errors
      If Left(Response, 6) = "ERROR:" Then
        CheckSuccess = False
        Exit Function
      End If
    
      'If the response includes the text code="200" then the
      'export was a success
      CheckSuccess = instr(Response, "code=""200""") <> 0
    End Function 
    

0 个答案:

没有答案