[ubuntu] installing Oracle java

unzip the java package into a folder under /opt/

sudo update-alternatives –install “/usr/bin/java” “java” “/opt/java/64/jdk1.8.0_77/bin/java” 1

sudo update-alternatives –set java /opt/java/64/jdk1.8.0_77/bin/java

sudo update-alternatives –install “/usr/bin/javac” “javac” “/opt/java/64/jdk1.8.0_77/bin/javac” 1

sudo update-alternatives –set javac /opt/java/64/jdk1.8.0_77/bin/javac

Advertisements

[java] check password against DB

To check the password against the database encoded value is simple, but it is not supported out of the box. You’ll have to convert the generated hash into hex format

		byte[] encoded = md.digest();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < encoded.length; i++) {
			sb.append(Integer.toString((encoded[i] & 0xff) + 0x100, 16).substring(1));
		}

Here’s a complete example:

public boolean validate(String username, String password) {
		final Query query = em.createQuery("SELECT u FROM User u WHERE u.username = :username");
		query.setParameter("username", username);

		List<User> foundUsers = (List<User>) query.getResultList();
		if (foundUsers == null || foundUsers.isEmpty()) {
			return false;
		}
		User foundUser = foundUsers.get(0);
		MessageDigest md = null;
		try {
			md = MessageDigest.getInstance("SHA");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		md.update(password.getBytes());
		byte[] encoded = md.digest();
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < encoded.length; i++) {
			sb.append(Integer.toString((encoded[i] & 0xff) + 0x100, 16).substring(1));
		}

		if (sb.toString().equals(foundUser.getPassword())) {
			return true;
		}

		return false;
	}

Java 8 straming API examples

For this list:

List<String> users = Arrays.asList("Alice", "Bob", "Bob", "Josh", "Zair", "Mat");

AveragingDouble

double avgLength = users.stream().collect(Collectors.averagingDouble(String::length));
System.out.println("Average length: "+avgLength);

Will produce:
Average length: 3.6666666666666665

Joining

String joined = users.stream().collect(Collectors.joining("|"));
System.out.println(joined);

Will produce:
Alice|Bob|Bob|Josh|Zair|Mat

Other Streaming API Examples
Filter
Here is an example of filtering:

  List<String> users = Arrays.asList("Alice", "Bob", "Josh", "Zair", "Mat");
    users
    .stream()
    .filter(x -> x.length() == 4)
    .forEach(x -> System.out.println(x));
  }

Map

List<String> uppercased =  users.stream().map(String::toUpperCase).collect(Collectors.toList());
System.out.println(uppercased);
//OR
uppercased =  users.stream().map(x -> x.toUpperCase()).collect(Collectors.toList());
System.out.println(uppercased);

MapToInt

   List<String> users = Arrays.asList("Alice", "Bob", "Josh", "Zair", "Mat");
    users
    .stream()
    .mapToInt(x -> x.length())
    .forEach(x -> System.out.println(x));
//OR
    users
    .stream()
    .mapToInt(String::length)
    .forEach(System.out::println);

Count

System.out.println(users.stream().count());

Distinct

List<String> distinctUsers = users.stream().distinct().collect(Collectors.toList());
usersunique.stream().forEach(System.out::println);

Limit

List<String> limited = users.stream().limit(4).collect(Collectors.toList());
    	System.out.println(limited);

[SOLVED] ERROR 1045 (28000): Access denied for user ‘root’@’localhost’ (using password: NO)

If you’ve just installed mysql community server (in my case on Fedora 23) and you’re getting:

ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)

The only thing that helped was:

edit your /etc/my.cnf file and add the line "skip-grant-tables" without quotes

After that you’ll be able to do:

 mysql -uroot -p

and type the new password. Then:

mysql> use mysql; mysql> UPDATE user SET password=PASSWORD("YOUR NEW PASSWORD HERE") WHERE User='root'; mysql> flush privileges; mysql> quit

By the way, if you login back into mysql and then try:

mysql> show databases;
ERROR 1820 (HY000): You must reset your password using ALTER USER statement before executing this statement.

You might want to:


mysql> SET PASSWORD=PASSWORD('your new password');
Query OK, 0 rows affected, 1 warning (0.00 sec)

 

[JAX-WS] Simple Dispatch Client Example

Here is an example of a Dispatch type Java SE WebService client.
1. A sample Web Service (Running as a Dynamic Web Application in Glassfish):

package com.vvirlan;
import javax.jws.WebService;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.xml.ws.soap.Addressing;
/**
 *
 * @author vladimir
 */
@Addressing(enabled = true, required = true)
@WebService(serviceName = "NewWebService")
public class NewWebService {

    @WebMethod(operationName = "hello")
    public String hello(@WebParam(name = "name") String txt) {
        return "Hello " + txt + " !";
    }
}

2. Here is the client (A simple Java SE application). Make sure to start the previous application first:

package dispatchclient;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.AddressingFeature;
import javax.xml.ws.soap.SOAPBinding;

/**
 *
 * @author vladimir
 */
public class DispatchClient {

    public static void main(String[] args) {

        QName port = new QName("http://vvirlan.com/", "PortName");
        Service service = Service.create(port);
        service.addPort(port, SOAPBinding.SOAP11HTTP_MTOM_BINDING, "http://localhost:8080/SimpleJaxWs/NewWebService");
        Dispatch<SOAPMessage> dispatch = service.createDispatch(port, SOAPMessage.class, Service.Mode.MESSAGE, new AddressingFeature());
        SOAPBinding binding = (SOAPBinding) dispatch.getBinding();
        
        binding.setMTOMEnabled(true);
        SOAPMessage request = null;
        try {
            request = MessageFactory.newInstance().createMessage();
            SOAPBody reqSoapBody = request.getSOAPBody();
            SOAPElement methodName;
            QName method = new QName("http://vvirlan.com/", "hello", "vvir");
            QName name = new QName("name");

            methodName = reqSoapBody.addChildElement(method);
            SOAPElement nameParam = methodName.addChildElement(name);
            nameParam.addTextNode("Vladimir");

        } catch (SOAPException e) {
            e.printStackTrace();
        }

        SOAPMessage response = dispatch.invoke(request);
        try {
            SOAPBody resSoapBody = response.getSOAPBody();
            System.out.println(resSoapBody.getTextContent());
        } catch (SOAPException e) {
            e.printStackTrace();
        }

    }
}

This will generate a SOAP request like:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:vvir="http://vvirlan.com/">
    <soapenv:Header/>
    <soapenv:Body>
        <vvir:hello>
            <name>Vladimir</name>
        </vvir:hello>
    </soapenv:Body>
</soapenv:Envelope>

And the response will be

<?xml version='1.0' encoding='UTF-8'?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:helloResponse xmlns:ns2="http://vvirlan.com/">
            <return>Hello Vladimir !</return>
        </ns2:helloResponse>
    </S:Body>
</S:Envelope>