?? releaseresourceservlet.java
字號:
/*
* The contents of this file are subject to the terms
* of the Common Development and Distribution License
* (the "License"). You may not use this file except
* in compliance with the License.
*
* You can obtain a copy of the license at
* http://www.opensource.org/licenses/cddl1.php
* See the License for the specific language governing
* permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL
* HEADER in each file and include the License file at
* http://www.opensource.org/licenses/cddl1.php. If
* applicable, add the following below this CDDL HEADER,
* with the fields enclosed by brackets "[]" replaced
* with your own identifying information:
* Portions Copyright [yyyy]
* [name of copyright owner]
*/
/*
* $(@)ReleaseResourceServlet.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
package com.sun.sjc.idtv.vod.server.topologymanager;
import javax.xml.soap.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.rmi.PortableRemoteObject;
import javax.ejb.*;
import javax.xml.transform.*;
import java.util.*;
import java.io.*;
import com.sun.sjc.idtv.vod.shared.data.*;
public class ReleaseResourceServlet extends HttpServlet {
static MessageFactory fac = null;
static {
try {
fac = MessageFactory.newInstance();
} catch (Exception ex) {
ex.printStackTrace();
}
};
static TopologyManager tm;
static TopologyManagerHome tmHome;
MessageFactory msgFactory;
public void init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
System.out.println("\n>>> ReleaseResource is executing at http://voyager/VOD/TopologyManager/releaseResource...");
javax.naming.InitialContext initContext = null;
Hashtable env = new java.util.Hashtable(1);
try {
initContext = new javax.naming.InitialContext();
String JNDIName = "java:comp/env/ejb/TopologyManager";
System.out.println("Looking up: " + JNDIName);
Object objref = initContext.lookup(JNDIName);
tmHome = (TopologyManagerHome)PortableRemoteObject.narrow(objref, TopologyManagerHome.class);
System.out.println("Creating the topologymanager bean");
tm = tmHome.create();
} catch (Exception e) {
throw new ServletException("Unable to connect to TopologyManager " + e.getMessage());
}
try {
// Initialize it to the default.
msgFactory = MessageFactory.newInstance();
} catch (SOAPException ex) {
throw new ServletException("Unable to create message factory " + ex.getMessage());
}
}
public void doPost( HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, IOException {
try {
// Get all the headers from the HTTP request.
MimeHeaders headers = getHeaders(req);
// Get the body of the HTTP request.
InputStream is = req.getInputStream();
// Now internalize the contents of a HTTP request and
// create a SOAPMessage
SOAPMessage msg = msgFactory.createMessage(headers, is);
SOAPMessage reply = null;
reply = onMessage(msg);
if (reply != null) {
// Need to call saveChanges because we're going to use the
// MimeHeaders to set HTTP response information. These
// MimeHeaders are generated as part of the save.
if (reply.saveRequired()) {
reply.saveChanges();
}
resp.setStatus(HttpServletResponse.SC_OK);
// putHeaders(reply.getMimeHeaders(), resp);
// Write out the message on the response stream.
OutputStream os = resp.getOutputStream();
reply.writeTo(os);
os.flush();
} else
resp.setStatus(HttpServletResponse.SC_NO_CONTENT);
} catch (Exception ex) {
throw new ServletException("JAXM POST failed "+ex.getMessage());
}
}
static MimeHeaders getHeaders(HttpServletRequest req) {
Enumeration enum1 = req.getHeaderNames();
MimeHeaders headers = new MimeHeaders();
while (enum1.hasMoreElements()) {
String headerName = (String)enum1.nextElement();
String headerValue = req.getHeader(headerName);
StringTokenizer values = new StringTokenizer(headerValue, ",");
while (values.hasMoreTokens())
headers.addHeader(headerName, values.nextToken().trim());
}
return headers;
}
static void putHeaders(MimeHeaders headers,
HttpServletResponse res) {
Iterator it = headers.getAllHeaders();
while (it.hasNext()) {
MimeHeader header = (MimeHeader)it.next();
String[] values = headers.getHeader(header.getName());
if (values.length == 1)
res.setHeader(header.getName(), header.getValue());
else {
StringBuffer concat = new StringBuffer();
int i = 0;
while (i < values.length) {
if (i != 0)
concat.append(',');
concat.append(values[i++]);
}
res.setHeader(header.getName(), concat.toString());
}
}
}
// This is the application code for handling the message.
public SOAPMessage onMessage(SOAPMessage message) {
SOAPMessage reply = null;
try {
//retrieve orderID from message received
SOAPBody receivedSB = message.getSOAPPart().getEnvelope().getBody();
Iterator receivedIt = receivedSB.getChildElements();
SOAPBodyElement receivedSBE = (SOAPBodyElement)receivedIt.next();
String requestType = receivedSBE.getElementName().getLocalName();
Iterator receivedIt2 = receivedSBE.getChildElements();
SOAPElement receivedSE = (SOAPElement)receivedIt2.next();
//get the cpeID
String cpeID = receivedSE.getValue();
//create reply message
//MessageFactory factory = MessageFactory.newInstance();
reply = fac.createMessage();
SOAPPart sp = reply.getSOAPPart();
SOAPEnvelope env = sp.getEnvelope();
env.getHeader().detachNode();
SOAPBody sb = env.getBody();
boolean ok = false;
Name newBodyName = null;
if(requestType.equals("release-qam-port")) {
System.out.println("onMessage(): releasing QAM port");
// get resource from topologymanager
ok = tm.release(cpeID);
newBodyName = env.createName("release-qam-port-reply","","http://vod-vas");
}
else if (requestType.equals("release-ip-port")) {
System.out.println("onMessage(): releasing IP port");
ok = tm.releaseIpResource(cpeID);
newBodyName = env.createName("release-ip-port-reply","","http://vod-vas");
}
SOAPBodyElement replySBE = sb.addBodyElement(newBodyName);
//status
Name statusName = env.createName("status");
SOAPElement status = replySBE.addChildElement(statusName);
if (ok) {
status.addTextNode("ok");
} else {
status.addTextNode("nok");
}
reply.saveChanges();
} catch (Exception ex) {
ex.printStackTrace();
}
return reply;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -