📄 licenseserver.java
字号:
si = new SessionInfo(userID, contentID, shopID, casFlag);
session.setAttribute("session", si);
session.setMaxInactiveInterval(600); //10 minutes
m_log.fine("Created a new session");
}
String sessionId = session.getId();
if (!si.isAuthenticated() && m_authenticate) {
// To authenticated the user redirect to Opera Proxy
//
m_log.finer("REDIRECTING TO OPERA PROXY");
m_log.fine("User is not authenticated");
String operaProxyURL = getOperaProxyURL(userID, contentID, shopID, sessionId);
m_log.fine("Redirecting to URL [" + operaProxyURL + "]");
m_log.info("OPERA PROXY URL:" + operaProxyURL);
if(mmiMessage != null) {
response.sendRedirect(operaProxyURL+"&"+mmiMessage.print("&"));
} else {
response.sendRedirect(operaProxyURL);
}
} else {
m_log.finer("SENDING LICENSE");
m_log.fine("User is authenticated");
sendLicense(userID, contentID, shopID, response);
}
m_log.finer("Leaving Function...");
}
/**
* Ensures that valid (non null and non empty) values have been
* provided for user, content and shop id.
*
* @param userID
* @param contentID
* @param shopID
* @return
* @throws LicenseServerException
*/
private boolean verifyLicenseRequestInput(String userID, String contentID, String shopID)
throws LicenseServerException {
m_log.finer("Entering Function...");
if (userID == null || "".equals(userID.trim())) {
m_log.finer("Servlet invoked with a null or empty user id value");
throw new LicenseServerException(
LicenseServerException.EC_INVALID_ARGUMENT,
"Missing userID for license request."
);
}
if (contentID == null || "".equals(contentID.trim())) {
m_log.finer("Servlet invoked with a null or empty content id value");
throw new LicenseServerException(
LicenseServerException.EC_INVALID_ARGUMENT,
"Missing contentID for license request."
);
}
if (shopID == null || "".equals(shopID.trim())) {
m_log.finer("Servlet invoked with a null or empty shop id value");
throw new LicenseServerException(
LicenseServerException.EC_INVALID_ARGUMENT,
"Missing shopID for license request."
);
}
m_log.fine("userID=[" + userID + "] contentID=[" + contentID + "] shopID=[" + shopID + "]");
m_log.finer("Leaving Function...");
return true;
}
/**
* Create the GET HTTP URL that will be used to
* redirect the client to opera proxy for authentication.
*
* @param userID
* @param contentID
* @param shopID
* @param sessionID
* @throws ServletException
* @throws IOException
*/
String getOperaProxyURL(String userID, String contentID, String shopID, String sessionID)
throws ServletException, IOException {
m_log.finer("Entering Function...");
StringBuffer operaProxyURL = new StringBuffer(m_operaProxyURL);
operaProxyURL.append("?" + Const.ACTION + "=" + Const.GET_LICENSE);
operaProxyURL.append("&" + Const.USERID + "=" + userID);
operaProxyURL.append("&" + Const.CONTENT_ID + "=" + contentID);
operaProxyURL.append("&" + Const.SHOPID + "=" + shopID);
operaProxyURL.append("&" + Const.SESSION_ID + "=" + sessionID);
m_log.finer("Leaving Function...");
return operaProxyURL.toString();
}
/**
* Handle request to verify signature
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
boolean verifySignature(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession(true);
SessionInfo si = (SessionInfo) session.getAttribute("session");
if (si == null) {
//Throw exception here
//Tried to send license to
//non-existent session
return false;
}
//Do all decrypting of LRM here
//and if all OK then send license
//In the simple version of VoucherGenerator,
//there is no decryption of LRM.
//If a secure VoucherGenerator is implemented,
//then the appropriate verfication code goes here
return true;
}
/**
* Send license through HTTP response
*
* @param userID
* @param contentID
* @param shopID
* @param response
* @throws ServletException
* @throws IOException
*/
void sendLicense(String userID, String contentID, String shopID, HttpServletResponse response)
throws ServletException, IOException {
m_log.finer("Entering Function...");
License lic = getLicenseFromDatabase(userID, contentID, shopID);
m_log.fine("Sending back the retrieved license");
if (lic == null) {
throw new LicenseServerException(LicenseServerException.EC_NO_ERROR_CODE,
"License not available in the database");
}
InputStream licStream = lic.getLicense();
if (licStream == null) {
throw new LicenseServerException(LicenseServerException.EC_NO_ERROR_CODE,
"license is not valid");
}
response.setContentType(lic.getMime());
BufferedOutputStream outS = new BufferedOutputStream(response.getOutputStream());
byte by[] = new byte[32768];
int index = licStream.read(by, 0, 32768);
while (index != -1) {
outS.write(by, 0, index);
index = licStream.read(by, 0, 32768);
}
outS.flush();
m_log.finer("Leaving Function...");
}
/**
* Extracts the encoded rights from the vouchers which are embedded in the HTTP request.
*
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
private com.sun.sjc.idtv.vod.shared.data.RightsInfo[] extractRightsInfoFromVouchers(HttpServletRequest request, HttpServletResponse response) throws IOException {
String[] temp = request.getQueryString().split("&voucher=");
if(temp.length == 1) {
// no vouchers found
return null;
}
String voucher = temp[temp.length-1];
m_log.finer("voucher: "+voucher);
BASE64Decoder decoder = new BASE64Decoder();
voucher = URLDecoder.decode(voucher, "UTF-8");
byte[] bytes = decoder.decodeBuffer(voucher);
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
ObjectInputStream is = new ObjectInputStream(bais);
com.sun.sjc.idtv.vod.shared.data.RightsInfo[] rightsInfo =null;
try {
rightsInfo = (com.sun.sjc.idtv.vod.shared.data.RightsInfo[])is.readObject();
is.close();
} catch(ClassNotFoundException cnfe) {
cnfe.printStackTrace();
}
m_log.finer("RIGHTSINFO: "+rightsInfo[0].userID+" "+rightsInfo[0].remainingRights);
return rightsInfo;
}
/**
* This method sends the generated MMI response back to the client through an HTTP response
*
* @param mmiMessageRequest
* @param com.sun.sjc.idtv.vod.shared.data.RightsInfo[]
* @return MMIMessage
*/
private void sendMMIMessageResponse(String userID, String contentID, String shopID, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
com.sun.sjc.idtv.vod.shared.data.RightsInfo[] rightsInfo = extractRightsInfoFromVouchers(request, response);
if(rightsInfo == null) {
m_log.finer("NO RIGHTS INFO FOUND");
} else {
m_log.finer("NUMBER OF RIGHTS INFO FOUND: "+rightsInfo.length);
}
HashMap hashMap = (HashMap) getServletContext().getAttribute("SessionHashMap");
MMIMessage mmiMessage = (MMIMessage) hashMap.get(request.getParameter("session_id"));
if(mmiMessage == null) {
m_log.finer("MMIMessage from Session is empty");
}
MMIMessage mmiResponse = generateRightsResponse(mmiMessage, rightsInfo);
response.setContentType("text/plain;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(mmiResponse.print("\n"));
out.flush();
out.close();
}
/**
* Retrieve a license from the database that belongs to the given
* userID, contentID and shopID combination.
*
* @param userID
* @param contentID
* @param shopID
* @return
* @throws LicenseServerException
*/
License getLicenseFromDatabase(String userID, String contentID, String shopID)
throws LicenseServerException {
m_log.finer("Entering Function..");
License lic = null;
m_log.finer("Requesting license for "+userID+" "+contentID+" "+shopID);
lic = License.getLicenseFromDatabase(userID, contentID, shopID);
m_log.finer("Leaving Function..");
return lic;
}
/**
* m_authenticate is useful for development and stand alone testing of the License Server.
* By setting the value of the configuration
* value <b>authenticate</b> in the configuration file <b> /WEB-INF/conf/license-server.xml
* to false we can disable the License Server from going to
* the Opera Infrastructure for authentication.
* A client of the license server should always be tested with authentication to
* ensure that it will work properly.
*
*/
private boolean m_authenticate = true;
private String m_operaProxyURL;
private String m_keyDirectory;
private DatabaseHelper m_databaseHelper;
private static Logger m_log = Logger.getLogger(LicenseServer.class.getName());
private SimpleVoucherGenerator m_voucherHandler = new SimpleVoucherGenerator();
private static final long serialVersionUID = 7735661521175646355L;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -