Archive for the 'Web' Category

 

Basic REST web service example ..

May 08, 2015 in Java, REST, Web, WebServices

From: http://getj2ee.over-blog.com/article-tutorial-rest-with-spring-mvc-and-maven-97283997.html

[java]

@Controller // –> Declare a Spring MVC Controller

@RequestMapping(“/computer”) // –> Map an URI with a controller or a method

@PathVariable // –> Read a variable in an Uri and assign this value to a java variable

@RequestBody // –> Declare a Pojo class to map the http request body

@ResponseBody // –> Declare a Pojo class to generate Json content to return to the http client

// Declaration as this class as a controller
@Controller

// Declaration that this controller handles requests on uri */computer
@RequestMapping(“/computer”)

public class ComputerController {

// Stores computers
private static final ComputerStorage computerStorage = new ComputerStorage();

// Declare a method that handle all GET request on */computer
@RequestMapping(method = RequestMethod.GET)

// Return a list of computer to the http client
@ResponseBody public List getComputers() {
return computerStorage.getAll();
}
}

[/java]

node.js learning with learnyounode .. solutions ..

May 08, 2015 in javascript, node.js

Exercise 4, asynchronous (async) file processing ..

From: https://github.com/JeffPaine/learnyounode-solutions

[js]

var countLines = function (err, fileBuffer){
// console.log(fileBuffer);
console.log(fileBuffer.split(‘\n’).length);
};

fs.readFile(process.argv[2], ‘utf8’, countLines);

[/js]

Javascript regular expressions (regex, regexp) .. city, state, zip (postal code) ..

Dec 14, 2011 in javascript, Web

Check for zip / postal code :

  if (field.value.search(/^\d{5}$/) != -1) {
    ...
  }

Check for “city, state zip” (space zip..) :

(note the 5\4 zip check.. like anybody would enter a 9 digit zip.. right..)

  if (field.value.search(/([^,]+),\s*(\w{2})\s*(\d{5}(?:-\d{4})?)/) != -1 ) {
    ...
  }

Check for “city, state” :

  if (field.value.search(/([^,]+),\s*(\w{2})/) != -1 ) {
    ...
  }

Href for JavaScript links: “#” or “javascript:void(0)”?

Nov 06, 2011 in javascript

From: http://stackoverflow.com/questions/134845/href-for-javascript-links-or-javascriptvoid0

When building a link that has the sole purpose to run JavaScript code, is it better to

<a href="#" onclick="myJsFunc();">Link</a>

or

<a href="javascript:void(0)" onclick="myJsFunc();">Link</a>

?

Geolocation webservices notes

Oct 11, 2011 in Geolocation, WebServices

Zip code to geolocation service (zip to geolocation, postal code to geolocation)

http://www.webservicemart.com/uszip.asmx/ValidateZip?zipcode=95014

Bing geolocation query example

From: http://msdn.microsoft.com/en-us/library/ff701714.aspx

http://dev.virtualearth.net/REST/v1/Locations?CountryRegion=US&adminDistrict=WA&locality=Somewhere&postalCode=98001&addressLine=100%20Main%20St.&key=BingMapsKey

Or using a python script to exercise and experiment :


import os

# python qbing1.py

BKEY = 'BlahBlahMyBingKeyBluhBluh.'

def makeurl1():
    burl = 'http://dev.virtualearth.net/REST/v1/Locations'
    burl += '?CountryRegion=US'
    burl += '\&adminDistrict=WA'
    burl += '\&locality=Somewhere'
    burl += '\&postalCode=98001'
    burl += '\&addressLine=100%20Main%20St.'
    burl += '\&key=' + BKEY

    return burl;

def makeurl2():
    burl = 'http://dev.virtualearth.net/REST/v1/Locations'
    burl += '?CountryRegion=US'
    burl += '\&locality=San%20Francisco'
    burl += '\&postalCode=94121'
    burl += '\&addressLine=529%2029th%20Ave.'
    burl += '\&key=' + BKEY

    return burl;

burl = makeurl2()

print 'URL: ' + burl

sts = os.system("wget -O qbing1.json " + burl )

Yahoo (webservices) query language

http://developer.yahoo.com/yql/console/

Creating a wlfullclient.jar for JDK 1.6 client applications..

Jul 30, 2010 in Java, Weblogic

From http://download.oracle.com/docs/cd/E12840_01/wls/docs103/client/jarbuilder.html

Use the following steps to create a wlfullclient.jar file for a JDK 1.6 client application:

1.Change directories to the server/lib directory.

cd WL_HOME/server/lib

2. Use the following command to create wlfullclient.jar in the server/lib directory:

java -jar wljarbuilder.jar

3.You can now copy and bundle the wlfullclient.jar with client applications.

4.Add the wlfullclient.jar to the client application’s classpath.

WebLogic 10.3 Workshop notes..

Jun 24, 2010 in Server, Weblogic

>> WebLogic 10.3 is old but I want to follow the workshop and I cannot
find the “workshop” in the Oracle WebLogic 10.3.3 package.

  * For page navigation, it uses Apache Beehive, which is now retired.

>> Add Eclipse components via:

  * /opt/bea/wlserver_10.3/common/bin/config.sh &

  * Fix eclipse java crash: add this line to “workshop.ini”:

  -Dorg.eclipse.swt.browser.XULRunnerPath=/usr/lib/xulrunner-1.9/xulrunner

  * Create application domains outside the server directory space

>> Fix jre error: “JRE is selected, but the path is invalid”.

  * Set path (JAVA_HOME, WLS_JAVA_HOME, etc..) in .project.properties

>> Web Application with Beehive (now retired) page navigation control

  >> See wlswswa*.png images.

  >> Main points/tools:

  * Page Flow editor and overview panels

  * Controller methods annotated for “page flow” processing.

>> Web Services

* Simple Web Service

– add src/services folder

– Add “SomeService.java” class to “src/services” folder.

– SomeService.java will be annotated with @WebService class annotation

– Web methods will be annotated with @WebMethod annot.

* To Test, right click on (the ‘services’ trigger ‘Run As’) SomeService.java and do ‘Run As’ with ‘Run On Server’ selection.

– The web service test page is displayed with URL:

http://host:7001/wls_utc/WebServiceProjectName/WebServiceName?WSDL

– The web service test client page:

http://host:7001/wls_utc/

>> To enable or disable the domain configuration locking feature in a development domain:

In browser, http://host:7001/console, log in as admin..

1. In the banner toolbar region at the top of the right pane of the Console, click Preferences.
2. Click User Preferences.
3. Select or clear Automatically Acquire Lock and Activate Changes to enable or disable the feature.
4. Click Save.

>> Entity EJBs (VisitBean) – Persistence with CMP (Container Managed Persistence)

– Create EJB project, Create package (under ejbModule), right-click on package, select “New Weblogic Entity Bean”.

– Screen shots are in wlswsejb[0-9]*_*.png

– Examine the generated code. Open the “Properties” view from top Eclipse menu “Window / Show View / Properties”. The “Properties” view should open in a window with a tab (bottom area).

– Click on the “@Entity” annotation. The allowed and the set attribute values for the annotation are shown in the “Properties” view (see wlswsejb3_properties_view.png).

– Setup CMP fields and methods:

@CmpField(column = "visitorName", primkeyField = Constants.Bool.TRUE)
@LocalMethod()
public abstract java.lang.String getVisitorName();

Note: primkeyField indicates whether the field is a primary key.

Helpful WebLogic error messages..

Jun 14, 2010 in Server, Weblogic

In WebLogic 10.3 ..

weblogic-ejb-jar.xml with this entity-descriptor:

<entity-descriptor>

<persistence>
<persistence-use>
  <type-identifier>WebLogic-CMP</type-identifier>
  <type-version>6.0</type-version>
  ...
</persistence-use>
</persistence>

</entity-descriptor>

Triggers this error (Note the -truly- useful help message):

Unable to deploy EJB: /opt/bea/user_projects/domains/base_domain/servers/AdminServer/stage
/_appsdir_ProductEjb_jar/ProductEjb.jar from ProductEjb.jar:

Persistence type 'WebLogic-CMP' with version '6.0' which is referenced in bean 'Product' is not 
installed. The installed persistence types are:(WebLogic_CMP_RDBMS, 5.1.0), 
(WebLogic_CMP_RDBMS, 7.0), (WebLogic_CMP_RDBMS, 6.0).

Change this:

  <type-identifier>WebLogic-CMP</type-identifier>
  <type-version>6.0</type-version>

to this:

  <type-identifier>WebLogic_CMP_RDBMS</type-identifier>
  <type-version>7.0</type-version>

and the jar will deploy.

JBoss vs WebLogic – JNDI issues..

Jun 11, 2010 in JBoss, Weblogic

In JBoss, one can get a reference to the Remote bean directly:

try
{
    context = new InitialContext(jndiprops);
    BookTestBeanRemote beanRemote = 
        (BookTestBeanRemote) context.lookup("BookTestBean/remote");
    beanRemote.test(); 
} catch (NamingException e) {
    ...
}

In WebLogic 10.3, I was not able to do the same thing.
The exception said that WebLogic JNDI failed to find the bean.
As some searches indicated, I should have used something like this for
the lookup string: “BookTestBean#com.blah.BookTestBeanRemote” ..
I tried all kinds of combinations but I was not able to make it work.

In JBoss, the “Home” interface is not required. In WebLogic, it is required.
This may have something to do with different levels of implementation
of the EJB spec but I would think JBoss 5 and WebLogic 10
should be pretty close in compatibility.. maybe.. what do I know?

Based on this, the only way I was able to make WebLogic work, was via
the “Home” interface:

Context context;
try
{
    context = new InitialContext(jndiprops);

    UserTransaction txn = (UserTransaction) context.lookup("javax.transaction.UserTransaction");
    if (txn != null) {
        String jndiLookupStr = "";
	// jndiLookupStr = "BookTestBean#de.laliluna.library.BookTestBeanRemote";
        jndiLookupStr = "BookTestBean";

        Object oref = context.lookup(jndiLookupStr);
        BookTestBeanHome homeRef = (BookTestBeanHome) PortableRemoteObject.narrow(oref,
           BookTestBeanHome.class);
        BookTestBeanRemote beanRef = homeRef.create();

        txn.begin();
        beanRef.test(); 
        txn.commit();
    } // txn != null
}
catch (Exception e) {
    e.printStackTrace();
}

This page says that remote reference retrieval is possible with WebLogic:

http://camelcase.blogspot.com/2009/04/how-to-call-remote-transactional.html

JBoss vs WebLogic notes – persistence..

Jun 11, 2010 in JBoss, Weblogic

In JBoss, this works:

List someBooks = em.createQuery("FROM Book b WHERE b.author=:name")
    .setParameter("name", "Sebastian").getResultList();

In WebLogic, I had to specify SELECTs:

List someBooks = em.createQuery("SELECT b FROM Book b WHERE b.author=:name")
    .setParameter("name", "Sebastian").getResultList();

If I have time, I will dig further and see why they behave differently but I suspect
that JBoss CMP is used automatically while in WebLogic, OpenJPA is used which
requires the “SELECT” syntax.

Good example on client transaction setup:

http://camelcase.blogspot.com/2009/04/how-to-call-remote-transactional.html