JavaEE call page issue on request parameter - java

I have belowcode in my POSTmethod of a page.
The problem is that I want to go to the url /news-management#tab_default_1 and forward the request too.
So when I use getRequestDispatcher() the request is forwarded but the URLis just /news_management
And when I use sendRedirect() the url is good but the request is not forwarded:
String del = request.getParameter("delete");
Base base = new Base();
if (request.getParameter("object").length() > 200)
request.setAttribute("bad", "Object length is too big");
if (request.getParameter("message").length() > 800)
request.setAttribute("bad", "Content length is too big");
if (del != null)
base.deleteNews(del);
if (request.getParameter("add") != null)
{
Newsletter nl = new Newsletter();
nl.setObject(request.getParameter("object"));
nl.setMessage(request.getParameter("message"));
base.addNews(nl);
}
this.getServletContext().getRequestDispatcher("/WEB-INF/news-management.jsp").forward(request, response);
//response.sendRedirect(request.getContextPath() + "/news-management#tab_default_1");
Any idea on how to get the desired behaviour?

Related

partial range requests from chrome causing error

I have tried to implement range request video playback on a system that has webservlet and UI, that sends the range request from chrome starting with bytes :0- to the backend dataserver. Now I have been sending the full stream as I was under the impression the jetty server handles the response range. I see it work for the first and second request but then fails as the next request has a range that is less than what the previous range was.
(Request 1) Range:bytes=0-
(Response 1) Accept-Ranges:bytes
Content-Length:6748748
Content-Range:bytes 0-10005/6748748
(Request 2) Range:bytes=6717440-
(Response 2) Accept-Ranges:bytes
Content-Length:6748748
Content-Range:bytes 6717440-6718465/6748748
(Request 3) Range:bytes=3932160-
(Response 3) Accept-Ranges:bytes
Content-Length:6748748
Content-Range:bytes 3932160-3933185/6748748
(Request 4) Range:bytes=5701632-
(Response 4) Fails -
Can anyone make sense of this? With short videos this does not occur, so is there some timeout issue but then why is the chrome request with a smaller range? This is what I specify in the headers but again do not explicitly send the bytes as requested as I thought jetty handles it.
if(inputStream != null) {
if (parameters.containsKey("Range")) {
String range =parameters.get("Range").toString();
String[] ranges = range.split("=")[1].split("-");
final int from = Integer.parseInt(ranges[0]);
if(parameters.containsKey("Content-Length")) {
int sLength = (int) parameters.get("Content-Length");
int to = 10005 + from;
if (to >= sLength) {
to = (int) (sLength - 1);
}
if (ranges.length == 2) {
to = Integer.parseInt(ranges[1]);
}
final String responseRange = String.format("bytes %d-%d/%d", from, to, sLength);
parameters.put("Responserange", responseRange);
}
}
EDIT:
My logs in the dataserver side show the following consistently with each request being handled:
java.nio.channels.ClosedChannelException,
Added additional stack trace
java.nio.channels.ClosedChannelException
at
org.eclipse.jetty.util.IteratingCallback.close(IteratingCallback.java:427)
at org.eclipse.jetty.server.HttpConnection.onClose(HttpConnection.java:489)
at org.eclipse.jetty.io.ssl.SslConnection.onClose(SslConnection.java:217)

How do I do a SIP telephone call

I want to constract a telephone-caller inside my java application. For this pupose I used a JAIN-SIP library. After the first INVITE the system needs Proxy-Authentication. The second invite is conscructed with the help of "AuthenticationHelperImpl.class":https://gitorious.org/0xdroid/external_nist-sip/source/1e0f37693341071f316852c8e05a08deef2b7fc4:java/gov/nist/javax/sip/clientauthutils/AuthenticationHelperImpl.java#L311, includes Proxy-Authentication header and lloks like:
INVITE sip:+11111111111#fpbx.de;maddr=fpbx.de SIP/2.0
Call-ID: 1c609509a43b721ab11c396c1e6ea9e7#192.168.17.107
CSeq: 2 INVITE
From: "77735hk6iu" <sip:77735hk6iu#fpbx.de>
To: "+111111111111111" <sip:+11111111111#fpbx.de>
Via: SIP/2.0/UDP 192.168.17.107:34567;rport;branch=z9hG4bK-383337-5bc4fd6b7a616843fce9eaa243bcb10e
Max-Forwards: 70
Contact: <sip:77735hk6iu#192.168.17.107:5060>
Content-Type: application/sdp
Proxy-Authorization: Digest username="77735hk6iu",realm="fpbx.de",nonce="VLaIxVS2h5muPS30F2zLdXHjup6ELyen",uri="sip:+111111111111#fpbx.de:5060;maddr=fpbx.de",response="47ea578c6b01c99fd3ed2b41c60983df"
Content-Length: 61
v=0
o=- 130565705777141827 1 IN IP4 192.168.17.107
s=call
After that I receive at the beginning code 100 message ("your call is very important for us") followed with 408 code message ("Request Timeout").
What I did to imporve the situation:
tried different phone number formats: 004930208488480,
04930208488480, 049, 0049, sdfhajfkhsk. For all these numbers I
become the same combination on messages.
tried to use port in request uri
tried to remove maddr from request uri.
tried to fullfill the message body with codek settings.
to set and remove rport from via header
If you now what I'm doing wrong, please, help me.
Thank you in advance.
I think, Maybe your Proxy-Authorization header is wrong. Maybe you is miscalculated. I wanted to share my resolve.
authUser is your phoneNumber. (for example: 77735hk6iu )
authPass is your user's password.
msg is your invite request.(Headers !)
AccountManagerImpl accountManagerImp = new AccountManagerImpl(authUser, AuthPass);
AuthenticationHelperImpl authenticationHelperImpl = new AuthenticationHelperImpl(accountManagerImp);
try {
this.authentication = authenticationHelperImpl.handleChallenge(msg, (SIPClientTransaction)trans);
AuthenticationHelperImple.java Class :
public AuthorizationHeader handleChallenge(Response challenge, ClientTransaction challengedTransaction) throws SipException {
SIPRequest challengedRequest = ((SIPRequest) challengedTransaction.getRequest());
ListIterator authHeaders = null;
if (challenge.getStatusCode() == Response.UNAUTHORIZED) {
authHeaders = challenge.getHeaders(WWWAuthenticateHeader.NAME);
}
else {
if (challenge.getStatusCode() == Response.PROXY_AUTHENTICATION_REQUIRED) {
authHeaders = challenge.getHeaders(ProxyAuthenticateHeader.NAME);
}
else {
throw new IllegalArgumentException("Unexpected status code ");
}
}
if (authHeaders == null) {
throw new IllegalArgumentException("Could not find WWWAuthenticate or ProxyAuthenticate headers");
}
WWWAuthenticateHeader authHeader = null;
while (authHeaders.hasNext()) {
authHeader = (WWWAuthenticateHeader) authHeaders.next();
String realm = authHeader.getRealm();
this.uri = challengedRequest.getRequestURI();
this.requestMethod = challengedRequest.getMethod();
this.requestBody = (challengedRequest.getContent() == null) ? "" : new String(challengedRequest.getRawContent());
if (this.accountManager instanceof SecureAccountManager) {
UserCredentialHash credHash = ((SecureAccountManager) this.accountManager).getCredentialHash(challengedTransaction,
realm);
if (credHash == null) {
logger.logDebug("Could not find creds");
throw new SipException("Cannot find user creds for the given user name and realm");
}
this.authorizationHeader = this.getAuthorization(requestMethod, uri.toString(), requestBody, authHeader, credHash);
}
else {
UserCredentials userCreds = ((AccountManager) this.accountManager).getCredentials(challengedTransaction, realm);
if (userCreds == null) {
throw new SipException("Cannot find user creds for the given user name and realm");
}
// sipDomain = userCreds.getSipDomain();
// we haven't yet authenticated this realm since we were
// started.
this.authorizationHeader = this.getAuthorization(requestMethod, uri.toString(), requestBody, authHeader, userCreds);
}
}
return this.authorizationHeader;
}
getAuthorization function :
public AuthorizationHeader getAuthorization(String method,
String uri,
String requestBody,
WWWAuthenticateHeader authHeader,
UserCredentials userCredentials) throws SecurityException {
String response = null;
String qopList = authHeader.getQop();
String qop = (qopList != null) ? "auth" : null;
String nc_value = "00000001";
String cnonce = "xyz";
try {
response = MessageDigestAlgorithm.calculateResponse(authHeader.getAlgorithm(),
userCredentials.getUserName(), authHeader.getRealm(),userCredentials.getPassword(), authHeader.getNonce(), nc_value, // JvB added
cnonce, // JvB added
method, uri, requestBody, qop,logger);
}
catch (NullPointerException exc) {
throw new SecurityException("The received authenticate header was malformatted: " + exc.getMessage());
}
AuthorizationHeader authorization = null;
try {
if (authHeader instanceof ProxyAuthenticateHeader) {
if (this.headerFactory != null) {
authorization = headerFactory.createProxyAuthorizationHeader(authHeader.getScheme());
}
else {
authorization = new ProxyAuthorization();
authorization.setScheme(authHeader.getScheme());
}
}
else {
if (this.headerFactory != null) {
authorization = headerFactory.createAuthorizationHeader(authHeader.getScheme());
}
else {
authorization = new Authorization();
authorization.setScheme(authHeader.getScheme());
}
}
authorization.setUsername(userCredentials.getUserName());
authorization.setRealm(authHeader.getRealm());
authorization.setNonce(authHeader.getNonce());
authorization.setParameter("uri", uri);
authorization.setResponse(response);
if (authHeader.getAlgorithm() != null) {
authorization.setAlgorithm(authHeader.getAlgorithm());
}
if (authHeader.getOpaque() != null) {
authorization.setOpaque(authHeader.getOpaque());
}
// jvb added
if (qop != null) {
authorization.setQop(qop);
authorization.setCNonce(cnonce);
authorization.setNonceCount(Integer.parseInt(nc_value));
}
authorization.setResponse(response);
} catch (ParseException ex) {
throw new RuntimeException("Failed to create an authorization header!");
}
return authorization;
}
Finally, your this.authentication variable is ProxyAuthorizationHeader. You must put this.authentication in your INVITE message. And than you will sent SipMessage from transaction or dialog to JAIN-SIP stack.
Good Luck !
The problem was partly solved when a removed "maddr=fpbx.de" from request URI and from proxy-auth. uri
fpr this a used handleCahllenge method with boolean arguments:
inviteTid = authenticationHelper.handleChallenge(response, tid, sipProvider, 15, **true**);
But I still don't know how I can a acchieve sponaneous telephone number.
The 100 message is hop-by-hop, that is to say it just means the next hop got your request. Other messages will typically be end-to-end (so, if you got a 180 Ringing, that typically means the endpoint being called sent the 180). A 408 typically shows up when one of the hops sent the INVITE but never got a response (and your SIP stack might be generating that internally when it doesn't get a provisional response in a reasonable timeframe -- usually about 32 seconds with the default SIP timers).
I don't know your network setup, but there are several private IPs in that message (of the 192.168.x.x variety). If I had to guess, your first hop is sending the 100 back to the IP/port it received it from, but the next response is following the Via headers (as it should), and the hop after you isn't respecting the rport parameter, so the response is getting lost. Alternately, your NAT is poorly configured and is closing the hole it created for the INVITE too quickly.
If you have a proxy on the edge of your network that this message is going out, it is either putting bad Via headers on the message (possibly with the internal IP instead of the external IP) or it is sending the INVITE to the wrong place (causing it to never get a response), and the 408 is coming from it.

Change default Service Unavailable message when REST api call fails

When user tries to do api call but if our server is down (due to many reasons) then my website UI gives following error:
Service Unavailable
The server is temporarily unable to service your request due to
maintenance downtime or capacity problems. Please try again later.
Additionally, a 406 Not Acceptable error was encountered while trying
to use an ErrorDocument to handle the request.
Apache/2.4.7 (Win32) OpenSSL/1.0.1e PHP/5.5.6 Server at localhost Port 80
I don't want this message to be this much detailed. I want to customize this message. How can I do this? Better if you suggest something change on my ajax request (from where I do api calls)
Here is the javascript from where I do all my api request (if it helps)
function request(url, request_type, datatype, callback, postdata){
if(typeof(postdata) == "object"){
postdata = JSON.stringify(postdata);
} else if(typeof(postdata) == "string") {
SY.setContentType("text/plain");
}
$.ajax({
beforeSend: function(xhr){
for(var key in requestHeaders){
if(key == ANOTHER_CONTENT_TYPE)
continue;
xhr.setRequestHeader(key, requestHeaders[key]);
}
var client_id = readCookie(CLIENT_ID);
var client_token = readCookie(CLIENT_TOKEN);
if(client_id != null && client_token != null){
xhr.setRequestHeader(CLIENT_ID, client_id);
xhr.setRequestHeader(CLIENT_TOKEN, client_token);
}
//if there is another content type defined then modify the xhr content-type header
var newContentType = requestHeaders[ANOTHER_CONTENT_TYPE];
if(newContentType != undefined) {
SyUtils.log("Changing request content type to ="+newContentType);
xhr.setRequestHeader("Content-Type", newContentType);
delete requestHeaders[ANOTHER_CONTENT_TYPE];
}
var loadingDivId = callback.loadingDivId;
if(loadingDivId){
$("#"+loadingDivId).addClass("loader-small");
}else{
$("body").append("<div id=\"loader\"></div>");
}
},
url: url,
data: postdata,
dataType: datatype,
type: request_type,
success: function(data) {
var loadingDivId = callback.loadingDivId;
if(loadingDivId){
$("#"+loadingDivId).removeClass("loader-small");
}else{
$("#loader").remove();
}
// Write back cookie again so that expiry time refreshes
var client_id = readCookie(CLIENT_ID);
var client_token = readCookie(CLIENT_TOKEN);
if(client_id != null && client_token != null){
saveCredentials(client_id, client_token);
}
callback("success", data);
},
error: function(xhr, status, error) {
var loadingDivId = callback.loadingDivId;
if(loadingDivId){
$("#"+loadingDivId).removeClass("loader-small");
}else{
$("#loader").remove();
}
try{
var errorMessageObj = JSON.parse(xhr.responseText);
}catch(err){
errorMessageObj = {type:"ERROR", message:xhr.responseText};
}
var st = xhr.status;
var currentPage = window.location.pathname; // to avoid infinite redirect loop
if (st == 401 && currentPage != "/login"){
SY.removeCurrentUser();
deleteCookie(CLIENT_ID);
deleteCookie(CLIENT_TOKEN);
window.location.replace("/login?toPage="+currentPage);
}else if(st == 403 && currentPage != "/unauthorized"){
window.location.replace("/unauthorized");
}else if(st == 500 && currentPage != "/error"){
window.location.href = "/error";
}else if(st == 404 && currentPage != "/404"){
window.location.href = "/404";
}else{
callback(error, errorMessageObj);
}
}
});
};

Viewing data on browser

i have a domain with fields like URL, lastModified etc. Now i want to view the information i get from this code into the list page. How can i do tht
def url = new URL("http://www.google.com")
HttpURLConnection connection = (HttpURLConnection)url.openConnection()
// long contentLength = Long.parseLong(connection.getHeaderField("Content-Length"));
connection.setRequestMethod("GET")
connection.connect()
connection.getURL()
println("Date: " + new Date(connection.getDate()))
println("LastModified Date:" + new Date(connection.getLastModified()))
if (connection.responseCode == 200 || connection.responseCode == 201){
def returnMessage = connection.content
//print out the full response
println "${returnMessage}";
System.out.println(connection.getURL());
} else {
println "Error Connecting to " + url
println "Couldn't connect to url"
}
From your controller, you can use the render method,
render returnMessage
(see http://grails.org/doc/2.0.x/ref/Controllers/render.html)
or pass it in a model to a gsp page and then print it from gsp,
return [returnMsg: returnMessage]
....(in .gsp)
${returnMsg}
(see http://grails.org/doc/2.0.x/guide/theWebLayer.html#GSPBasics)

HtmlUnit webpage status code

I am trying to get the web status for a given page. However when its a 404 error, the page does not return the status code, rather it throws and error.
int status= webClient.getPage("website").getWebResponse().getStatusCode();
System.out.println( status);
Any Ideas?
I am looking to see when sites time out, however for testing purposes I malformed the url of the desired website to see if I can even see a 404.
According to this
You can do this:
webclient.setThrowExceptionOnFailingStatusCode(False)
****EDIT ***
This does print out your status code:
WebClient webClient = new WebClient();
webClient.setThrowExceptionOnFailingStatusCode(false);
int status = webClient.getPage("http://google.co.uk/ffffff").getWebResponse()
.getStatusCode();
System.out.println(status);
Prints out 404 - your status code.
Alternatively, you can continue to allow the FailingHttpStatusCodeException to be thrown (true). Then within the catch clause get the error status code.
...
int status = 0;
Object page = null;
try {
page = webClient.getPage(webRequest);
webClient.close();
if (page instanceof UnexpectedPage) {
status = ((UnexpectedPage) page).getWebResponse().getStatusCode();
} else if (page instanceof HtmlPage) {
status = ((HtmlPage) page).getWebResponse().getStatusCode();
}
// do something else ...
} catch (FailingHttpStatusCodeException | IOException e) {
if (e instanceof FailingHttpStatusCodeException) {
status = ((FailingHttpStatusCodeException) e).getStatusCode();
}
// do something else ...
}

Categories