Java HTTP Get Request - java

I need to make one HTTP GET Request to a Java Web Service:
I'm making this request:
http://127.0.0.1:8080/MyService/services/service?method=myMethod&a=&b=test&startDate=2011-03-10 10:00&endDate=2011-03-10 19:00
When I Debug my app the parameters come with the values switched. I already tried to encode the startDate and endDate parameters but the result is the same.
What am i doing wrong?

You should encode your URLs so they don't contain spaces. This could be your problem. See http://download.oracle.com/javase/1.5.0/docs/api/java/net/URLEncoder.html

As #Mirkules says, you should encode the spaces and colons in the parameter values ... even if you don't think it makes any difference!
Apart from that, maybe your servlet code is expecting Request.getParameters() to deliver the parameters in the order that they appear in the URL. This is not the case. If order of the parameters is significant, you need to parse the query string yourself. (Or consider fixing your web API so that the query parameter order is irrelevant.)

Related

Java serialization of a query to list of strings

I stumbled upon a strange issue and cant find quick solution to deal with it. Front side of my application sends query to back-end I added square brackets for readability.
as it should be
http://localhost:3000/?params=[First_value],[Anhui Jantene Hone Textiles Co., Ltd.],[Third_Value]
as it is
http://localhost:3000/?params=[first_value],[Anhui Jantene Hone Textiles Co.], [Ltd.],[Third_Value]
params are serialized to List but there is a problem when given value on a front-side has comma in the name, then instead of one value I get 2 entries. In a given example expected size of a list should be 3 but I get 4 elements.
Any help is appreciated
You can't do this in the query string because of how a comma is treated. You need to use a POST with a request body like this:
["First_value", "Anhui Jantene Hone Textiles Co., Ltd.","Third_Value"]

Declaring configuration of custom configurable application in java?

So for a hobby project of mine, I would like to create an application that translates an HTTP call and request between two services.
The application does that based on a configuration that can be set by the user. The idea is that the application listens to an incoming API call translates the call and then forwards it.
Then the application waits for a response then translates the response and sends it back to the caller.
A translation can be as simple as renaming a field value in a body object or replace a header field to the body.
I think a translation should begin with mapping the correct URL so here is an example of what I was thinking of a configuration should look like:
//request mapping
incoming URL = outgoing URL(
//Rename header value
header.someobject.renameto = "somevalue"
//Replace body object to header
body.someobject.replaceto.header
)
I was thinking that the configuration should be placed in a .txt file and read by the application.
My question is, are there other similar systems that use a configuration file for a configuration like this? And are there other/better ways to declare a configuration?
I have done something sort-of-similar in a different context (generate code from an input specification), so I will provide an outline of what I did to provide some food for thought. I used Config4* (disclosure: I developed that). If the approach I describe below is of interest to you, then I suggest you read Chapters 2 and 3 of the Config4* Getting Started Guide to get an overview of the Config4* syntax and API. Alternatively, express the concepts below in a different configuration syntax, such as XML.
Config4* is a configuration syntax, and the subset of syntax relevant to this discussion is as follows:
# this is a comment
name1 = "simple value";
name2 = ["a", "list of", "values"];
# a list can be laid out in columns to simulate a table of information
name3 = [
# item colour
#------------------
"car", "red",
"jeans", "blue",
"roses", "red",
];
In a code generator application, I used a table to provide rules to specify how to generate code for assigning values to fields of messages. If no rule was specified for a particular field, then some built-in rules provided default behaviour. The table looked something like the following:
field_rules = [
# wildcarded message.field instruction
#----------------------------------------------------------------
"Msg1.username", "#config:username",
"Msg1.password", "#config:password",
"Msg3.price", "#order:price",
"*.account", "#string:foobar",
"*.secondary_account", "#ignore",
"*.heartbeat_interval", "#expr:_heartbeatInterval * 1000",
"*.send_timestamp", "#now",
];
When my code generator wanted to generate code to assign a value to a field, the code generator constructed a string of the form "<message-name>.<field-name>", for example, Msg3.price. Then it examined the field_rules table line-by-line (starting from the top) to find a line in which the first column matched "<message-name>.<field-name>". The matching logic permitted * as a wildcard character that could match zero or more characters. (Conveniently, Config4* provides a patternMatch() utility operation that provides this functionality.)
If a match was found, then the value in the instruction column told the code generator what sort of code to generate. (If no match was found, then built-in rules were used, and if none of those applied, then no code was generated for the field.)
Each instruction was a string of the form "#<keyword>:optional,arguments". That was tokenized to provide the keyword and the optional arguments. The keyword was converted to an enum, and that drove a switch statement for generating code. For example:
The #config:username instruction specified that code should be
generated to assign the value of the username variable in a runtime
configuration file to the field.
The #order:price instruction specified that code should be generated
to assign the value returned from calling orderObj->getPrice() to the field.
The #string:foobar instruction specified the string literal foobar
should be assigned to the field.
The #expr:_heartbeatInterval * 1000 instruction specified that code should
be generated to assign the value of the expression _heartbeatInterval * 1000
to the field.
The #ignore instruction specified that no code should be generated to
assign a value to the field.
The #now instruction specified that code should be generated to assign
the current clock time to the field.
I have used the above technique in several projects, and each time I have invented instructions specific to the needs of the particular project. If you decide to use this technique, then obviously you will need to invent instructions to specify runtime translations rather than instructions to generate code. Also, don't feel you have to shoehorn all of your translation-based configuration into a single table. For example, you might use one table to provide a source URL -> destination URL mapping, and a different table to provide instructions for translating fields within messages.
If this technique works as well for you as it has worked for me on my projects, then you will end up with your translation application being an "engine" whose behaviour is driven entirely by a configuration file that, in effect, is a DSL (domain-specific language). That DSL file is likely to be quite compact (less than 100 lines), and will be the part of the application that is visible to users. Because of this, it is worthwhile investing effort to make the DSL as intuitive and easy-to-read/modify as possible, because doing that will make the translation application: (1) user friendly, and (2) easy to document in a user manual.

Its possible send array in url

I would like to know if i can send an array of this type endate [] in the parameter fecha2 of the following url:
http://www.xxxx.com/app/disponibilidad?idUser&fecha2&fecha1&desde&hasta
endate[] has several date I want to send in a single url. I would like to know if that is possible
You can get a good idea in How to pass an array within a query string?
You can also encode it to JSON and pass it as a single string, then decode it on your backend. Sounds simpler to me.

A special case of accessing parameters from servlet where they are encrypted and contain &,= value

I need to implement a URL which looks something like below one. The Notable thing is that its only one parameter i need to access and the value that's supposed to be held by the parameter is encrypted which may contains characters like & and =.
Example 1.
www.abc.com/disp?v=qww78agd=
The parameter v in above url contains value qww78agd=.
Supposing the encryption turns out the following way.
Example 2.
www.abc.com/disp?v=qww7&f=iuy68=
www.abc.com/disp?v=qww7&f==iuy68=
Then servlet will take v and f as two separate parameters, whereas i want to obatain qww7&f==iuy68= from parameter.
I thought of taking up all key value pairs from request HashMap and concatenating the joints via & and =. But the problem is that two consecutive = signs as in second case of example 2, its treated as only one equal to in request key,value map. Hence, my other = sign(s) are lost.
Is there any way such that i can get query string part as it is on my servlet and parse it using string processing on my own?
or any other approach?
It was easy.
Using the request.getQueryString() method the query string portion is fetched as it is as string on the servlet which I processed using my own logic of string processing.

On Google App Engine is it possible JDOQL Date without using a parameterised query?

Is it possible to do a date query in JDOQL without using a parameterrised query on Google App Engine.
I am trying to write some generic code that looks something like this, where criteria is just a string, and I would like to be able to specify anything - with this piece of code not needing to to know much about the underlying data.
Query query = pm.newQuery(tClass);
if (criteria!=null) {
query.setFilter(criteria);
}
criteria could be "startdate = 'someproperlyformatteddatetime'"
Thanks for your suggestions.
Of course, GAE JDO queries support JDOQL. You could simply do something like this: q.setFilter("height <= 200") or q.setFilter("name == 'Smith'"), where you would programmatically assemble the JDOQL filter string. The only downside is that you need to know the type of parameters (as saved in Datastore), as strings need to be enclosed in single or double quotes.
Note that all restrictions on queries still apply.
Also, if you want to query on multiple properties where you also use inequality operator, then you need to define compound indexes beforehand.
Update: JDOQL literal parameter specification works with string and numeric values; all other value types must use parameter substitution. You could still do that programmatically.
Another workaround would be if you use long instead of Date and convert dates to UNIX timestamps (which are of type long).

Categories