Big Data and Data Analytics

In this post, you can find the presentation that I prepared for data analytics and big data.

In case of any questions, please write me 🙂

Click here for Big Data Analysis

Advertisements

How to send the location information with an Android application including address

Hello again. In this article I will explain how to implement an Android application that uses location objects and retrieves the information for location such as altitude, latitude or longitude, gets an address information belong to those numbers and sends it with an sms to emergency peoples that were chosen before.

First of all, we need to know about the objects and interfaces that we need to use. We need to use “LocationListener” interface to implement methods for location class. Of course we will extend to “Activity” class to implement activities such as onclick, onselect, etc.

We call a LocationManager object. It is the object where we can call the providers. Providers are the services that can be chosen as network, gps or passive providers. Three of them can be arranged to be used according to what is needed and how they are needed. If no internet is accesible, gps would be the best choice, and if network is available, network will work with better results. Passive provider uses the latest information from the cache.

Screenshot from 2016-06-19 21:33:44

Methods are used according to those declarations. Also we need to declare permissions and design the interface of the application. The code for that can be found below.

package com.example.root.multiple_contacts;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.location.Address;
import android.location.Criteria;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.Settings;
import android.telephony.SmsManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends Activity implements LocationListener{

    Context context = null;                         //context provider
    String message;                                 //first message that will be sent
    String message2;                                //second message that will be sent (sending more than 160 characters with sms provider is not possible
    int PICK_CONTACT;
    ContactsAdapter objAdapter;                     //contacts to be displayed
    ListView lv = null;
    EditText edtSearch = null;
    LinearLayout llContainer = null;
    Button btnOK = null;

    Button btnDelete = null;
    RelativeLayout rlPBContainer = null;
    ArrayList<String> selected = new ArrayList<String>();       //selected contact names
    ArrayList<String> numbers = new ArrayList<>();         //selected contact numbers
    Location location;                                     //location object
    List<String> adress = new ArrayList<>();               //address information from geocoder


    @Override
    protected void onCreate(Bundle savedInstanceState) throws SecurityException {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LocationManager locationmanager;
        locationmanager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);

        locationmanager.requestLocationUpdates(locationmanager.GPS_PROVIDER,2000,10,this);
        boolean isNetworkEnabled = locationmanager.isProviderEnabled(locationmanager.NETWORK_PROVIDER);
        boolean isGPSEnabled = locationmanager.isProviderEnabled(locationmanager.GPS_PROVIDER);

        System.out.println("Activation of network provider is: "+isNetworkEnabled);
        System.out.println("Activation of the gps provider is "+isGPSEnabled);
        Criteria cri=new Criteria();
        String provider=locationmanager.getBestProvider(cri,true);

        this.onLocationChanged(location);
        location=locationmanager.getLastKnownLocation(provider);

        System.out.println(locationmanager.getLastKnownLocation(provider));
        context = this;

        rlPBContainer = (RelativeLayout) findViewById(R.id.pbcontainer);
        edtSearch = (EditText) findViewById(R.id.input_search);
        llContainer = (LinearLayout) findViewById(R.id.data_container);
        btnOK = (Button) findViewById(R.id.ok_button);
        //preventing sliding
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);

        String cityName = "Not Found";
        Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
        try {

            List<Address> addresses = gcd.getFromLocation(location.getLatitude(),location.getLongitude(), 10);
            for (Address adrs : addresses) {
                if (adrs != null) {

                    String city = adrs.getAddressLine(0);

                        cityName = city;
                        adress.add(cityName);
                    System.out.println(adress);

                        System.out.println("city ::  " + cityName);


                    }
                    // // you should also try with addresses.get(0).toSring();



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


        final Button sendmessage=
       (Button) findViewById(R.id.btnSendSMS);
        sendmessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    if (selected.isEmpty())
                        Toast.makeText(getApplicationContext(), "No contact was chosen for messaging!", Toast.LENGTH_LONG).show();
                    else{

                    message = "Help! I am in an emergency!My latitude is: " +
                            location.getLatitude() + "And my longitude is: " + location.getLongitude() + ". Please" +
                            "check the map for my location. ";
                                  sendmessage(message);
                        message2 = "My precious address is:" +adress.toString();
                        sendmessage(message2);
                    /*ArrayList<String> messages = new ArrayList<String>();

                    for(int i=0;i<selected.size();i++){
                        message = "Help, "+selected.get(i)+"! I am in an emergency!My latitude is: " +
                                location.getLatitude() + "And my longitude is: " + location.getLongitude() + ". Please" +
                                "check the map for my location.";
                        messages.add(message);
                        sendmessage(messages.get(i));
                    }*/
                    Toast.makeText(getApplicationContext(), "Help message has sent!", Toast.LENGTH_LONG).show();}


                }

                catch(Exception e){
                    Toast.makeText(getApplicationContext(),"Send failed. Try not to die.", Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }
            }
        });


        btnOK.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                getSelectedContacts();
            }
        });

        btnDelete = (Button) findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {

                if(!selected.isEmpty()){
                for(int i=0;i<selected.size();i++){
                    selected.remove(selected.get(i));

                    for(int j=0;i<numbers.size();i++){
                        numbers.remove(numbers.get(j));
                    }
                }
                    Toast.makeText(context, "Some item(s) are deleted",
                            Toast.LENGTH_SHORT).show();}

                else
                    Toast.makeText(context, "No items to delete!",
                            Toast.LENGTH_SHORT).show();

            }
        });

        edtSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                                      int arg3) {
                // When user changed the Text
                String text = edtSearch.getText().toString()
                        .toLowerCase(Locale.getDefault());
                objAdapter.filter(text);

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start,
                                          int count, int after) {

                for (ContactObject bean : ContactsListClass.phoneList) {

                    if (bean.isSelected()) {
                        selected.add(bean.getName());
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable arg0) {
                // TODO Auto-generated method stub
            }
        });

        addContactsInList();

    }

    private void getSelectedContacts() {

        StringBuffer sb = new StringBuffer();

        for (ContactObject bean : ContactsListClass.phoneList) {

            if (bean.isSelected()&&!selected.contains(bean.getName())&&!numbers.contains(bean.getNumber())){
                selected.add(bean.getName());
                numbers.add(bean.getNumber());

            }
        }


        for (int i = 0; i < selected.size(); i++) {
            sb.append(selected.get(i));
            sb.append(",");
        }


        String s = sb.toString().trim();

        if (TextUtils.isEmpty(s)) {
            Toast.makeText(context, "Select at least one Contact",
                    Toast.LENGTH_SHORT).show();
        } else {

            s = s.substring(0, s.length() - 1);
            Toast.makeText(context, "Selected Contacts : " + s,
                    Toast.LENGTH_SHORT).show();

        }


    }

    private void addContactsInList() {

        Thread thread = new Thread() {
            @Override
            public void run() {

                showPB();

                try {

                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null, null, null, null);

                    try {
                        ContactsListClass.phoneList.clear();
                    } catch (Exception e) {

                    }

                    while (phones.moveToNext()) {
                        String phoneName = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                        String phoneNumber = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String phoneImage = phones
                                .getString(phones
                                        .getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID));


                        ContactObject cp = new ContactObject();


                        cp.setName(phoneName);
                        cp.setNumber(phoneNumber);
                        cp.setImage(phoneImage);

                        ContactsListClass.phoneList.add(cp);

                    }
                    phones.close();

                    lv = new ListView(context);

                    lv.setLayoutParams(new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT,
                            RelativeLayout.LayoutParams.MATCH_PARENT));

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // TODO Auto-generated method stub
                            llContainer.addView(lv);
                        }
                    });

                    Collections.sort(ContactsListClass.phoneList,
                            new Comparator<ContactObject>() {
                                @Override
                                public int compare(ContactObject lhs,
                                                   ContactObject rhs) {
                                    return lhs.getName().compareTo(
                                            rhs.getName());
                                }
                            });

                    objAdapter = new ContactsAdapter(MainActivity.this,
                            ContactsListClass.phoneList);
                    lv.setAdapter(objAdapter);
                    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                        @Override
                        public void onItemClick(AdapterView<?> parent,
                                                View view, int position, long id) {

                            CheckBox chk = (CheckBox) view
                                    .findViewById(R.id.contactcheck);
                            ContactObject bean = ContactsListClass.phoneList
                                    .get(position);
                            if (bean.isSelected()) {
                                bean.setSelected(false);
                                chk.setChecked(false);
                            } else {
                                bean.setSelected(true);
                                chk.setChecked(true);
                            }

                        }
                    });

                } catch (Exception e) {

                    e.printStackTrace();

                }

                hidePB();

            }
        };
        thread.start();

    }

    void showPB() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                rlPBContainer.setVisibility(View.VISIBLE);
                edtSearch.setVisibility(View.GONE);
                btnOK.setVisibility(View.GONE);
            }
        });
    }

    void hidePB() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                rlPBContainer.setVisibility(View.GONE);
                edtSearch.setVisibility(View.VISIBLE);
                btnOK.setVisibility(View.VISIBLE);
            }
        });
    }


    public void sendmessage (String mesaj)
    {
        SmsManager menager1;
        menager1 = SmsManager.getDefault();
        for(int i=0;i<numbers.size();i++){
        menager1.sendTextMessage(numbers.get(i),null,selected.get(i)+" ,"+mesaj,null,null);
        System.out.println(numbers.get(i));
        System.out.println(selected.get(i));}

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        return true;
    }
    @Override
    public void onLocationChanged(Location location) {


    }
    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {
    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

        Toast.makeText(context,"Your location provider is disabled, opening settings",Toast.LENGTH_LONG).show();
        Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        startActivity(intent);
    }

}

Manifest file can be found below;

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.root.multiple_contacts"
    android:versionCode="1"
    android:versionName="1.0" >


    <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_CONTACTS"></uses-permission>
    <uses-permission android:name="android.permission.INTERNET"></uses-permission>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"></uses-permission>
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/emergency"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>


</manifest>

 

Main interface(activity_main.xml) can be found like following;

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
    android:background="#ad9494"
    >

<EditText
    android:id="@+id/input_search"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:hint="Search your contacts"
    android:textSize="25dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false"
    android:background="#b54e4e"
    android:padding="15dp"
    android:paddingTop="5dp"
    android:paddingBottom="5dp" />

<LinearLayout
    android:id="@+id/data_container"
    android:layout_width="match_parent"
    android:layout_height="260dp"
    android:gravity="center|top"
    android:orientation="vertical"
    android:weightSum="1"
    android:layout_below="@+id/input_search"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="false">

</LinearLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="DELETE CONTACTS"
        android:id="@+id/btnDelete"
        android:background="#caabab"
        android:layout_alignTop="@+id/ok_button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"

        android:paddingLeft="15dp"
        android:paddingRight="15dp" />


    <RelativeLayout
    android:id="@+id/pbcontainer"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#55000000"
    android:clickable="true"
    android:visibility="gone" >

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />
</RelativeLayout>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="PANIC!"
        android:id="@+id/btnSendSMS"
        android:background="#af0c0c"
        android:textSize="30dp"
        android:paddingLeft="30dp"
        android:paddingRight="30dp"
        android:paddingTop="15dp"
        android:paddingBottom="15dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"

        android:textStyle="bold"
        android:layout_marginBottom="10dp" />
    <Button
        android:id="@+id/ok_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="ADD CONTACTS"
        android:textSize="15dp"
        android:background="#caabab"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="20dp"
        android:layout_alignBottom="@+id/btnSendSMS"
        android:layout_marginBottom="90dp" />

</RelativeLayout>



 

Also, one must add the icon for the application that will be displayed while launching and to be shown on the screen and also one must choose the contact pictures and add it to /res/drawable. My picture name was yellow.png so it can be changeable. And also the app icon is in /res/mipmap.

Overall, this application will help you to send your information from locationmanager and get the address from geocoder and send it to a receiver that has been choosen before sending.
That is all, if you have any further questions, please ask or tell me any kind of reviews.

Text Processing with Haskell and Python

In this article i will be calculating some basic things with Python and Haskell. The text file is from here.

Requirements:

1)Number of words

2)Number of sentences

3)Number of question marks

4)Number of unique words

5)Number of paragraphs

6)First paragraph of every section

7)Last paragraph of every section

Part1:Haskell

Haskell is a functional language and its logic is almost the base of all other languages in terms of treating everything as a function and evaluating functions.

It is extremely shortcut to find the requirements with haskell because they are straightforward and implemented inside the haskell. My code is like following;

Screenshot from 2015-12-27 10:46:45

Basically, main role is “filter” method. It filters the strings or strings inside the files and output whatever is desired.

Import clause is to import the library Data.List to indicate that we will be working with lists(to calculate paragraphs, words,etc) and this will happen by putting them into an array of strings.

putStr is to print a text into command line.

length is to calculate the size of the arrays.

I used Hugs to compile my code. Hugs is an editor to run the haskell files and haskell codes. For example, if i want to load a file into hugs and run the haskell code inside of it it will be enough to type :l <file-directory> to run the code. In my case, i used to run /tmp/count.hs with runhugs command. It is something similar to the following;(number of unique words are being calculated in the picture)

Screenshot from 2015-12-27 11:13:16

For question mark, filter filters the text with question marks(?), for sentence it filters for the dots(.), for words it filters words clause, for paragraphs it filters null lines came after a text and for unique words it uses nub function to add everything to a set to deliminate the duplicates of every word in the text.

Part2: Python

Python is a very reliable, flexible and usable language among the others. It can compile so many things without having any trouble in any operating system.

I previously installed lamp server and that included Python so i didn’t make any additional installation to my computer. Typing python to command line will be enough to open the python editor. Running our Python code is like doing a similar thing to Haskell case. Writing python <file-directory> is enough to run the python code in the gedit file. The commented code is like following for the first 5 requirements. They are very simple in terms of writing a single for loop and incrementing the values in that for loop. Since loops are not allowed in Haskell, we didn’t do that.

 

Screenshot from 2015-12-27 11:20:15Screenshot from 2015-12-27 11:21:37

 

 

For the sixth and seventh requirement, i saved the indexes of every subtitle for every chapter of the book and divided the book into subranges of string arrays. Getting every array’s first and last element is enough to do the requirement. And every paragraph is recorded into another file to see them more clearly since command line is not enough to see all of them in a row. That file is like following;

Screenshot from 2015-12-27 11:23:40

Since there are lots of unique words(around 100k for that book), calculation for both of them is taking so long in both Haskell and Python. Especially in Haskell it is so much slower.

Unique words file is also like following. It can be tested whether it has a duplicate in that array by searching a specific element in the file. I did that and didn’t find any duplicates so the code is working!

Screenshot from 2015-12-27 11:25:55
Any comments of suggestions would be very appreciated. See you soon!

A Library System with PHP

Hello again. In this article i will explain how to write a web interface with PHP, JavaScript, MySQL, CSS and HTML.

This project is developed by using WAMP server. It is a very useful tool that combines Apache, MySQL and PHP servers in which everybody can install easily and do some useful work.

First of all, we need to start all the services after installing WAMP server. After that it will be placed like following;Screenshot (2)

localhost bar will take you directly for php projects. phpmyadmin will be for phpmyadmin to control the database and the flow of data. www directory will be the place that php project files will be held in. Press start all services button.

For PHP, an editor is needed. For that reason, i used CodeLobster software to compile by code but Notepad++ or other compilers will also work. It will be enough just to place that file in the www directory after all the coding is done.

The user guide about the website can be found in here. Besides that i will explain in detail how i writed the code.

First of all, we need to connect MySQL and PHP. That is quite easy, we just have to enter some parameters about our connection information(username and password for MySQL) and the name of our database which includes our tables like following;

 

Screenshot (3)

data.php is to be used whenever a query is needed to be executed for inserting, deleting or updating the informations about the student and books.

Next, we need to design our main page. In the main page, all the students are listed and all the operations can be done by clicking the buttons. The interface is like following;

Screenshot (4)

 

All the operations listed are possible with the library system. Every single operation is done by queries and their execution with some PHP and JavaScript codes. I have 3 tables. One is to hold students’ attributes and the other one is to hold books’ values, last one is to keep every students’ book name with student’s id.

For adding a book button,it adds into the book table but if we want to add a book to a student, we have to insert the book into the database first. A student can pick any book from the listed books. If a student is going to get a book, return a book or is to be printed all the books, edit must be clicked to get to the operations part. It is like following;

Screenshot (5)

The code of that is like following;

Screenshot (6)

This is the code for choosing the operation. Link is to get the themes for the interface(green and blue colors, changed size of text boxes,etc.). At form, we implicitly indicate that we will be using a variable in the code in “adding_book.php” page and we will do that by using “$_GET[‘<variable_name>’]” declaration. $student_id is received with that way from the edit.php page and that value returns the student that was chosen from the list at the main page. We will be sending this value to adding_book.php page to use to add into the database with the name of the book. That method is used through every php page since variables are important and they are needed for making the operations.

If we click on “ADD BOOK”, we will be facing with a screen that shows the book names that are available for the student. By choosing that, student will be added with its id and with that book name into the studentbook table and when necessary, we will get the books of that specific student.

We can also delete a book from the student with the same method, the only difference will be in the query. Instead of using insert, we will be using delete.

Screenshot (7)

As can be seen in here, both the variables are reached by get method and they will be inserted into student like following;

Screenshot (8)

The logic is all the same for all every other procedures. It is all about writing simple queries, passing them as parameters into PHP functions and making them work in an efficient way.

That will be all for this project. The other party of the code can be reached here.I would be very glad with every questions or corrections. Happy codings!

 

 

 

 

 

A basic JSF application to manage Freeradius-2

Now it is time to examine xhtml files. First of all, you need to have a welcome file list. Please add a figure like following;

<welcome-file-list>
    <welcome-file>SOMETHING-COMES-HERE.XHTML</welcome-file>
</welcome-file-list>

You can find my web.xml file in jsfapplication/src/main/webapps/webinf.

Webapp folder is where you add your xhtml files.

-Right click to the webapps folder.

-Click new

-Click HTML File

-Choose xhml file format then enter the name of the xhtml file. This will be where you write your html code with Primefaces themes.

We must be careful when we write tags. We must put h before tag names else the themes will not be seen. Such as;

<h:head>
    <title>ADD USER</title>
</h:head>

In my xhtml files, i used panels, datatables, outputlabels, inputtexts, command buttons and select check boxes. You can find my xhtml files in jsfapplication/main/webapp.

Now let’s examine the necessary tables for our database. As i mentioned in my first article, there is a one-to-many relationship between a user and role. That is why i created a cross table that has the id of a user and the id of a role. The structure is

CREATE TABLE 
(
    userId UNSIGNED_INT NOT NULL,
    roleId NOT NULL
);

And we need the role table like following;

CREATE TABLE Roles(
roleId INT PRIMARY KEY NOT NULL AUTO_INCREMENT,
roleName VARCHAR(100)
);

And for the users, we are going to use radcheck table.

For this relationship, i created an extra dao to manage the actions more efficiently.The queries for the cross table( the insertions and deletions) are done in this dao. And we use this table to get the roles of all the users. To prevent the dublicates, i used maps and sets in the dao of user.

Finally, i will explain the unit tests. First of all my reference book was junit recipes by rainsberger, a very straightforward textbook that explains how to write test classes of a jsf project.

First, you need to add Groovy plugin and dependencies.Add the dependency like following;

<dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.3.0</version>
</dependency>

Add the plugin like following;

Go to Files-Settings-Plugins and if there is no groovy, please press + button to add Groovy.

After that you should be getting a package under your base package called groovy. That package will be the place for your test classes.

You can write test classes or methods for everything in your project. For constructors, functions, queries, objects, getters and setters, etc. The main idea is that you should test what is not supposed to happen. If it does, the test class is working; which means there is something wrong with your testcase.

The structure is easy. When you right click to your class and say create test, it will automatically be opened. Your test class should be extended to GroovyTestCase . And the return types of your functions will be in the format of

assertEquals(<expected parameter>,<actual parameter>)

You can examine my test classes that i have written for possible errors in jsfapplication/main/java/Groovy.

That will be all for my project. If you have any concerns,  please reach out to me i would be happy to get the critizations from you. Enjoy your coding!

A basic JSF application to manage Freeradius-1

Hi again. In this article i will give a detailed explanation about my application. The code for the project will be citated to my github account in some paragraphs(https://github.com/aktasrozerin/jsfapplication). It is something that one can manage the radius server users and clients. It is just the beginning part i am still working around it, when i make changes, i will upload them immediately.

Let’s start from stratch. What we need now is to create a maven project. By the way i will be using intellij-idea but it is similar to Eclipse and NetBeans anyway. After creating a maven project, the rest is about fill the blanks 🙂

Now we need to create our base package. You can name it as you wish. That package will be where you put your objects, beans, data access objects, services, html pages, etc. You need to add the base package information into application-context.xml file like

<context:component-scan base-package="radius"/>

pom.xml is where we put our plugins, repositories and dependencies. You can copy my pom.xml file into yours or you can use other features according to your application. You can find my pom.xml file in the comments below the page.

For now my project has user feature but soon i will also upload client feature. For user feature, i have one adapter class called UserAdapter. You can find this class in jsfapplication/src/main/java/radius/adapters/UserAdapter.

The users also have some roles. Those roles can be added from the web application interface. It is also in jsfapplication/src/main/java/radius/adapters/RoleAdapter. A user may have more than one role, so between a user and role, there is a one-to-many relationship. If you have freeradius installed in your computer, that means you have a database. It’s name doesn’t matter but you will have to add a few tables more for that reason. First of all please add MySQL to your project. At the rightmost of the screen there is a bar called “database“.

-Click on it and press Alt+Insert to choose a database.

-Click data source then MySQL.

-Enter your freeradius database name into database name part. Also add your MySQL username and password and check connection.

Now it is time to introduce the beans. The beans are the objects for html pages. Every html function and data must be defined in those beans. You can define as many beans as you like. As long as it has service connection with @ManagedProperty. You can see my beans in jsfapplication/src/main/java/radius/Beans.

Next, you are going to need converters. If you don’t have converters, you will get errors when you pass bean parameters in your html pages. They will be understood as strings instead of objects. You will pass those converter classes in your html files to prevent that error. You can also find my converters in radius package.

After that we now have data access objects(daos). For now i have three daos but it will be more. In those daos i defined data sources(connections with MySQL), jdbctemplates(objects that executes queries) and methods that will be done in web application such as insert,update and print. See DAO package for daos and their interfaces in radius package.

You have to include every single dao into your application-context.xml file. This is a Spring Config file please don’t get mixed. It’s name can be something else as long as it is included in web.xml. One example for addition is like following;

<bean id="JDBCUserDao" class="radius.DAO.DaoImp.JDBCUserDaoImp">
    <property name="dataSource" ref="dataSource" />
</bean>

And also please add your MySQL connection informations like following after your beans in application-context.xml file.

<bean id="dataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
       <property name="url" value="jdbc:mysql://localhost:3306/radius"/>
       <property name="username" value="root"/>   //you don't have to be a root
       <property name="password" value=""/>   //type your password in here
</bean>

In the next article i will explain xhtml files, other tables and unit tests. Hope to see you there too 🙂

Deploying web applications with Tomcat Web Application Manager

In the previous article, i mentioned about deploying web applications with docker. In this one, i will be talking about working in localhost. Tomcat has an interface in which one can manage all the configurations, applications and setting easily. All we have to do is to create users. But of course to do that, our application must be using tomcat server.

Let’s start configuring our web application. If you have created a maven project, please right click to pom.xml and open settings.xml. It will be containing something like this;

<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">


</settings>

You should add a proxy and a server for your web application. Please write a code like the following before </settings>

<servers>
    <server>
        <id>tomcat7</id>  //my tomcat is tomcat7
        <username>rozerin</username>
        <password>PASSWORD</password> //type a password in here
    </server>
</servers>
<proxies>
    <proxy>
        <id>optional</id>
        <active>true</active>
        <protocol>http</protocol>
        <username>rozerin</username>
        <password>PASSWORD</password> //type a password in here 
        <host>proxy.xxx.com</host>
        <port>8080</port>
        <nonProxyHosts>xxx.com</nonProxyHosts>
    </proxy>
</proxies>

For that part you are done. Now it is time to make arrangements for tomcat server.

Please open tomcat folder. You will see a conf folder. Open that too. You will see a tomcat-users.xml file. We must edit that file. Open the command line and enter into that file typing;

sudo nano <path_to_your_tomcat-users.xml_file>

Screenshot from 2015-10-07 17:00:41

Make sure that you have defined a manager-script role. In the password part, please write your password you have written in settings.xml file. And also make sure that your usernames are the same.

Now it is time to test our configurations. Open your web browser and type

localhost:8080/manager

or

127.0.0.1:8080/manager

You will be getting and authentication screen. Type your username and password. If there is no error you will get the interface for tomcat like following;

Screenshot from 2015-10-07 17:04:12

If you see this, you are done. The topic of my previous article was to deploy applications. At the bottom of the page, one can upload the war files easily. If you don’t, please check your tomcat-users.xml and settings.xml files. Probably the mistake is somewhere in those files or check your catalina logs.

That is all for now. Hope to see you in my next article 🙂

Deploying JSF applications with Docker

Hello again. In this article i will explain how to run an application on a docker container. Docker is an open-source project that automates the deployment of applications inside software containers build from images. This gives the opportunity of flexibility and portability for submitted applications. The management is extremely easy. It is managed on linux command line. For more information and more commands, please see Docker official page https://www.docker.com/

To deploy an application with docker, one has to use docker containers. Docker containers can be pulled by docker run or docker pull command. If the image is not installed for that container, Docker will install it automatically. Or the images can be created manually according to the necessary software.

Before we start, i feel like i need to give some pre-informations about the usage of docker. The relation between a container and an image can be taught as a sculpture and mold. The container is sculpture and the image is mold. Which means a container is made of an image and to run an application on docker, one needs to create a container from an image. When docker creates the container automatically, that is pretty much the way it achieves that goal. More than one container can run at the same time. If you want to see your containers, please type on the command line

docker ps

You will see your running containers. If nothing is listed, this means that there is no container running at the current moment. Please type

docker ps -a

If you haven’t created any container, this list will be empty too. Don’t worry, when we create our image, we also will create our container. Now it is time to create our image.

As i said, the images can be created manually. This is what we will do. If you want to create an image, you have to be familiar with dockerfiles. A dockerfile is a file that keeps the necessary information about associated image and container. And it has a specific language for its own. For example, there is a RUN command that runs the application or file that is typed. There are many commands for dockerfile but we will be dealing with only a few of them.

In our example, i will try to run a JSF application which is configured with a tomcat server from a docker container. Now we need the .war file of our application. You can use ant build to get your file or you can create it from the command line of the compiler or you can execute a goal for building it. My compiler is intellij idea but that is pretty the same for the other Java compilers. If you are using idea, please type

execute maven goal

in the search button. Make sure the checkbox that says “include non-project items” is selected. Select it. Make sure that your project path is typed in the first line. In second line, type

war:war

After the execution, go to your target class. You will see a war file. If that is not created, try to see if there is any error by typing

mvn build tomcat7

or

Look at the rightest side of the window. You will see “Maven Projects“. There are options like clean, install and compile. Try to find the error by double clicking those options. In the command line or in the logs, you can find the errors easily.

Now we need to copy that war file into tomcat files. Please open your tomcat folder. It is in the location where you installed it. In that folder, try to find webapps. This is the folder where you will copy your war file. But from the interface that is not possible you have to do this operation from command line. Please type in the command line

cp <your .war file location> <your webapps location>

Before you ensure that the war file is there, please do not continue. This is a must.

Now it is time to create our image. Please type in the command line

sudo nano Dockerfile

there will an empty page appear if you haven’t created a dockerfile before. Below you can find my dockerfile.

Screenshot from 2015-10-07 16:12:54

FROM command is to include the base image from public repositories(since docker is an open-source software).This command is a must.

RUN command is to run the necessary commands for the container. In my file, i am trying to run tomcat and the war file. Just like writing in the command line.

EXPOSE is to allocate the necessary port. Since we are running a web application, it is 8080.

ENV is for assignments. Sets the http port to 8080.

WORKDIR is to give an appropriate path to application.

CMD is to execute the container. There can only one cmd command in a dockerfile. If you insert more than one, the last one will be executed(tomcat is run from catalina.sh).

Now it is time to create our image from that dockerfile. Please type

docker build -t <name_of_your_image> .

Please don’t forget to put a dot at the end of the command. You must eventually see “Succesfully build <id_of_the_image>”

Next, we need to create our container. Please type

docker run -p <a_port_number>:8080 -d <name_of_your_image>

Instead of <a_port_number> we could have wrote 8080, but since it is allocated by tomcat, we need to assign a different port. Now type docker ps -a to see your container. It must be something like in the following picture;

Screenshot from 2015-10-07 16:31:58

Look at the top-most container. It is the port that i am running. For now we are done.

Without docker, we can run our application with localhost:8080. When it is docker, we need to use docker’s ip address.

Please type

ifconfig

in the command line. At the top-most, you must be seeing a docker part. You can see docker ip.

Open your web browser now. Please type

<your_docker_ip>:8080/<name_of_your_application>

You should be seeing your application there.My output is something like that ( It is an application that gets the users for radius server as input and lists them. You can see that application in https://github.com/aktasrozerin/jsfapplication ) Yours should be seen as following;

Screenshot from 2015-10-07 17:51:23

If there is any error, i would be happy to help. That is the way we deploy our applications in a better and efficient way with docker.

That is all for this article, hope to meet you again and enjoy your coding!

Configuring freeradius (radius server) with mysql in ubuntu

Remote Authentication Dial-in User Service (RADIUS)

Hello everyone. In this article i will explain the configuration of radius server. What we need for the beginning is mysql and freeradius installed in your computer.One can configure radius with radiusd but i will go on with freeradius.

We define radius server as the traffic controller for any kind of “device” for AAA(Authentication, Authorization and Accounting). This means that we need to have at least one user and one client at once.

Freeradius is a very simple yet efficient tool that enables you to manage radius server. We can start by installing freeradius. Please type the following command to the command line

sudo apt-get install freeradius

Then please start freeradius by

sudo service freeradius start

As i said we need users and clients. That is the main goal of radius. To enable all the users in freeradius, we need to make a few settings in configuration files. First we need to define user(s). Please type in the command line

sudo nano /etc/freeradius/users

A file will appear with full of commanded sentences and properties. Declaration syntax of a user is extremely easy. Just type

<username> Auth-Type := EAP, Cleartext-Password := “<password>”

Screenshot from 2015-09-30 14:32:48

This is my users file yours should be something similar to this. After you write your declarations, just press F3 for saving then CTRL+X to exit the file.

Next, we need to declare our clients. There may be just one or hundreds of users and they can be localhosts, cisco devices(NAS),etc.

We will do almost the same thing that we did for users. Please open the clients file by following;

sudo nano /etc/freeradius/clients.conf

Again a file will appear full of commands with #. What you need to do is to declare clients in a format lie following, but be careful with #s because they may interfere your code. The syntax is easy again;

client NAME{

ipaddr=IPADDRESS

secret=SECRET

shortname=SHORTNAME}

or

client IPADDRESS{

secret=SECRET

shortname=SHORTNAME}

secret is something like a password for radius. When you make an authentication, your program will need that secret to confirm your device. Overall, your clients.conf file must be something like this;

Screenshot from 2015-09-30 14:43:58

You can declare all the clients that way. Not just the localhost. You may even get another virtual machine as client and type it’s IP address to the file.

That is all for configurations. But how will you check that the server is working? How will you see the errors and debug them? Those are also very easy. You can open freeradius in debug mode by typing

freeradius -X

in another terminal to see the logs and errors. At the starting, please don’t forget to do this, when you see

“Ready to process requests”

you are ready to start working with freeradius.

Now it is time for connection of mysql. First we need to create a database for our radius. Let’s call that database radius. Again please ensure that mysql is installed in your computer. Open mysql by typing

sudo mysql -u root -p

Enter your “mysql” password not your pc’s password. You should be seeing mysql command program in the terminal.Type

 CREATE DATABASE radius;

It will be saying something like database created. Then type exit to quit mysql for now.

Next, we need to install the necessary tables for our freeradius. Please type

sudo apt-get install freeradius freeradius-mysql

You will receive 7 tables in your radius database. See that they are installed correctly. Open mysql again and type

USE radius;

SHOW TABLES;

You should be seeing something like

Screenshot from 2015-09-30 14:55:32

Mines are much more but you will only need radacct, radcheck,radgroupcheck,radgroupreply,radpostauth,radreply and radusergroup. Some of them will be working for you when you want to create “a group” of users and radcheck is for all the users and radreply is the output of the logs which we will use to see the output of our tests.

We are almost ready now. When we add or remove a user or client, our tables will be effected directly. This is the way that freeradius is managed.

Now it is time to test our configurations. We are going to use radtest command for freeradius. The syntax is like the following;

radtest <username> <password> <hostname> <port> <secret>

or

radtest <username> <password> <hostname> 10 <secret>

or

radtest <username> <password> <hostname> 0 <secret>

In my case for localhost, my radtest is command like the following;

radtest abc 123 localhost 1812 testing123

testing123 is my secret for localhost client abc is my username 123 is my password localhost is my hostname and 1812 is the default port number for radius.

When you see “Access-Accepted” in the below you are done. If you see something like access declined or access rejected you need to debug your freeradius to see any kind of mistake that you did wrong or forgot to add. My mistakes were mostly from the database so be careful when you are declaring your database,tables and variables. You can test freeradius by adding users or clients by hand to mysql or from phpmyadmin easily. Debug mode of freeradius is pretty straightforward and understandable so i suggest you to use it efficiently.

That is all for this article. Please let me know for any kind of questions or knowledges.